gpt4 book ai didi

javascript - 类型错误 : Cannot read property 'then' of undefined in testing AngularJS controller with Karma

转载 作者:行者123 更新时间:2023-11-29 18:09:54 25 4
gpt4 key购买 nike

我正在尝试为从服务获得 promise 的 Controller 编写单元测试,但在 Jasmine 中我得到:

TypeError: Cannot read property 'then' of undefined

在 Controller 中,我根据服务返回的 promise 调用 .then()

有人可以帮忙吗?我肯定遗漏了一些明显的东西。

这是一个小插曲:http://plnkr.co/edit/vJOopys7pWTrTQ2vLgXS?p=preview

'use strict';

describe('Language controller', function() {
var scope, translationService, createController;

beforeEach(function() {
var mockTranslationService = {};
module('dictApp', function($provide) {
$provide.value('translationService', mockTranslationService);
});

inject(function($q) {
mockTranslationService.languages = [
{
name: 'slovak'
},
{
name: 'czech'
}
];

mockTranslationService.languagesWithWords = [{
name: 'slovak',
words: [{
key: 'Car',
translation: 'Auto',
createdOn: 0
}, {
key: 'Flower',
translation: 'Kvet',
createdOn: 0
}]
}, {
name: 'czech',
words: {
key: 'Plaza',
translation: 'Namesti',
createdOn: 0
}
}];

mockTranslationService.getLanguages = function() {
var deferred = $q.defer();

deferred.resolve(this.languages);
return deferred.promise;
};
});
});

beforeEach(inject(function($controller, $rootScope, _translationService_) {
scope = $rootScope.$new();
translationService = _translationService_;
createController = function () {
return $controller('LanguageCtrl',
{$scope: scope, translationService: translationService});
};
scope.$digest();
}));

it('should get the language array', function() {
spyOn(translationService, 'getLanguages');
createController();
expect(translationService.getLanguages).toHaveBeenCalled();
});
});

这是 Controller :

   dictControllers.controller('LanguageCtrl', ['$scope', 'translationService', function($scope, translationService){


$scope.getLanguages = function() {
translationService.getLanguages().then(function(){
$scope.languages = translationService.languages;
});
};

$scope.getLanguages();

$scope.getWords = function(language) {
translationService.getWords(language);
};


$scope.newWord = {};

$scope.addWord = function(language) {
translationService.addWord($scope.newWord, language);
$scope.newWord = {};
};

}]);

最佳答案

改用 spyOn(mockTranslationService, 'getLanguages').and.returnValue(deferred.promise);。在调用 getLocations() 之后而不是在 getLocations() 返回 promise 之前解决 promise 也更有意义。您还应该检查语言。

inject(function($q) {
deferred = $q.defer();

mockTranslationService.languages = [
{
name: 'slovak'
},
{
name: 'czech'
}
];

mockTranslationService.languagesWithWords = [{
name: 'slovak',
words: [{
key: 'Car',
translation: 'Auto',
createdOn: 0
}, {
key: 'Flower',
translation: 'Kvet',
createdOn: 0
}]
}, {
name: 'czech',
words: {
key: 'Plaza',
translation: 'Namesti',
createdOn: 0
}
}];

mockTranslationService.getLanguages = function () {
};

spyOn(mockTranslationService, 'getLanguages').and.returnValue(deferred.promise);

});

it('should get the language array', function() {
createController();

deferred.resolve();
scope.$digest();

expect(translationService.getLanguages).toHaveBeenCalled();
expect(scope.languages).toEqual(translationService.languages);
});

Plunkr

关于javascript - 类型错误 : Cannot read property 'then' of undefined in testing AngularJS controller with Karma,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28407031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com