javascript – Jasmine控制器测试,预期间谍被调用

我有一个在AngularJS控制器中定义的方法,在初始化时调用.我想用Jasmine测试它(“jasmine-core”:“^ 2.3.4”,“karma”:“^ 0.12.37”).我在互联网和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;
            });

        }]);
}());

茉莉花测试:

(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

06002

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

解决方法

在将模型传递到实例化控制器后,您正在监视模拟.

试试这个:

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

    beforeEach(module('app'));

    beforeEach(inject(function ($rootScope,_$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注意到我的解决方案中的错误

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...