TypeError: Cannot read property 'then' of undefined in testing AngularJS controller with Karma

TypeError: Cannot read property 'then' of undefined in testing AngularJS controller with Karma

我正在尝试为从服务获得承诺的控制器编写单元测试,但在 Jasmine 中我得到:

TypeError: Cannot read property 'then' of undefined

在控制器中,我根据服务返回的承诺调用 .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();
    });
});

这是控制器:

   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() returns 承诺之前解决承诺也更有意义。您还应该检查语言。

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