javascript - Jasmine Controller 测试,预计 spy 已被调用

标签 javascript angularjs karma-jasmine

我在 AngularJS Controller 中定义了一个方法,该方法在初始化时调用。我想使用 Jasmine ("jasmine-core": "^2.3.4", "karma": "^0.12.37") 测试它。我遵循了 Internet 上的一些教程和 StackOverflow 问题,但找不到正确的答案。请看一下这段代码:

Controller usersAddUserController:

(function () {
    'use strict';

    angular.module('app.users.addUser')
        .controller('usersAddUserController', ['$scope', 'usersAddUserService', function ($scope, usersAddUserService) {

            usersAddUserService.getCountryPhoneCodes().then(function (phoneCodes) {
                $scope.phoneCodes = phoneCodes;
            });

        }]);
}());

Jasmine 测试:

(function () {

    'use strict';

    describe('usersAddUserControllerUnitTest', function () {
        var scope, deferred, objectUnderTest, mockedAddUserService;

        beforeEach(module('app'));

        beforeEach(inject(function ($rootScope, $q, $controller) {
            scope = $rootScope.$new();

            function emptyPromise() {
                deferred = $q.defer();
                return deferred.promise;
            }

            mockedAddUserService = {
                getCountryPhoneCodes: emptyPromise
            };
                     
            objectUnderTest = $controller('usersAddUserController', {
                $scope: scope,
                usersAddUserService: mockedAddUserService
            });
        }));

        it('should call getCountryPhoneCodes method on init', function () {
            //when            
            spyOn(mockedAddUserService, 'getCountryPhoneCodes').and.callThrough();
            
            deferred.resolve();

            scope.$root.$digest();
            
            //then
            expect(mockedAddUserService.getCountryPhoneCodes).toHaveBeenCalled();
        });

    });
}());

运行测试后,错误信息为:

PhantomJS 1.9.8 (Windows 7 0.0.0) usersAddUserControllerUnitTest should call getCountryPhoneCodes method on init FAILED

    Expected spy getCountryPhoneCodes to have been called.

我显然遗漏了一些东西,但我无法弄清楚它是什么。任何帮助将不胜感激。

最佳答案

在将模拟传递到实例化 Controller 后,您正在监视模拟。

试试这个:

describe('usersAddUserControllerUnitTest', function () {
    var scope, deferred, objectUnderTest, mockedAddUserService, $controller;

    beforeEach(module('app'));

    beforeEach(inject(function ($rootScope, $q, _$controller_) {
        scope = $rootScope.$new();

        function emptyPromise() {
            deferred = $q.defer();
            return deferred.promise;
        }

        mockedAddUserService = {
            getCountryPhoneCodes: emptyPromise
        };

        $controller = _$controller_;
    }));

    function makeController() {    
        objectUnderTest = $controller('usersAddUserController', {
            $scope: scope,
            usersAddUserService: mockedAddUserService
        });
    }

    it('should call getCountryPhoneCodes method on init', function () {
        //when

        spyOn(mockedAddUserService, 'getCountryPhoneCodes').and.callThrough();
        makeController();

        deferred.resolve();

        scope.$root.$digest();

        //then
        expect(mockedAddUserService.getCountryPhoneCodes).toHaveBeenCalled();
    });

});

编辑 感谢@juunas 注意到我的解决方案中的错误

关于javascript - Jasmine Controller 测试,预计 spy 已被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31201921/

相关文章:

javascript - 如何获取以下语句的高度 - angular.element ('<div>content</div>' )?

javascript - 使用 ng-repeat 从 angularjs 中的 http 响应数组中检索数据

karma-runner - 如何从 karma 代码覆盖率报告中排除文件?

angular - 如何在 Angular Jasmine 规范中模拟路由器

javascript - 是否可以使用纯粹的 javascript 登录来抓取网站 - 在客户端

javascript - 从 Excel 读取并创建图表

javascript - 过滤/选择特定类型的数据

javascript - 如何使用 jQuery 更改特定的 HTML 字符串

angularjs - 将查询参数从 angular js 发布到 java rest 服务器

angular - Uncaught Error : Cannot find module "tslib"