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

前端之家收集整理的这篇文章主要介绍了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注意到我的解决方案中的错误

原文链接:https://www.f2er.com/js/158381.html

猜你在找的JavaScript相关文章