在 angularjs 控制器中对 promise 调用进行单元测试时遇到问题

Having problems unit testing a promise call in angularjs controller

SupportedLanguagesServicesget方法returnsapromise在controller中解析如下:

    angular.module('App').controller('MyController', ['SupportedLanguagesService',
    function(SupportedLanguagesService) {

        var self = this;
        self.supportedLanguages = [];

        SupportedLanguagesService.get().success(
            function(response) {
                self.supportedLanguages = response;
        });

    }]);

这是我写的测试,但它不起作用:

    describe('Controller: MyController', function() {

    beforeEach(module('App'));

    var rootScope, controller;

    beforeEach(inject(function($controller, $rootScope, SupportedLanguagesService, $q) {

        var deferred = $q.defer();
        deferred.resolve([{name:"English", code:"en"},{name:"Portugues", code:"pt_BR"]);
        spyOn(SupportedLanguagesService, 'get').andReturn(deferred.promise);

        rootScope = $rootScope;
        controller = $controller;
    }));

    it('should get SupportedLanguages', function() {
        rootScope.$digest();
        var ctrl = controller('MyController');
        expect(ctrl.supportedLanguages.length).toEqual(2);
    });

});

它在语句上抛出异常:var ctrl = controller('MyController');

感谢您的帮助。

代替 success(这是一个 $http 回调),您可以更改为 then,它适用于所有承诺。这将使您可以轻松地模拟承诺(而不用担心 $httpBackend:

    SupportedLanguagesService.get().then(
        function(response) {
            self.supportedLanguages = response.data;
    });

然后,您需要使用控制器的构造函数,然后然后调用一个$digest。所以,切换到这个应该会让你到达那里:

it('should get SupportedLanguages', function() {

    var ctrl = controller('MyController');
    rootScope.$digest();
    expect(ctrl.supportedLanguages.length).toEqual(2);
});

您还可以使用 $q.when:

简化设置代码
var response = [{name:"English", code:"en"},{name:"Portugues", code:"pt_BR"}];
spyOn(SupportedLanguagesService, 'get').andReturn($q.when(response));