角测试类型错误

Angular test Type Error

本文关键字:错误 类型 测试类 测试      更新时间:2023-09-26

已经在karma、摩卡茶和sinon试过了。

我得到一个错误,一旦我使用的服务。这是我的错误。请帮忙。

AssertionError: expected undefined to deeply equal 'strong'
        at /var/www/html/Testing/mocha/node_modules/chai/chai.js:210
        at assertEql (/var/www/html/Testing/mocha/node_modules/chai/chai.js:784)
        at /var/www/html/Testing/mocha/node_modules/chai/chai.js:3854
        at /var/www/html/Testing/mocha/www/index-controller.test.js:22
PhantomJS 1.9.8 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.043 secs / 0.002 secs)

这是我的indexcontroller.js

'use strict';
angular.module('beatso.index-controller', [])
    .controller('IndexController', function(
        commanService) {
        (function(vm){
            angular.extend(vm, {
                checkPassword: checkPassword
            })
            vm.headingTop = "Beatso A Music Fanatic Group";
            vm.password = "verystrogpassword";
            function checkPassword() {
                return commanService.passwordValidator("vm.password");
            }
        })(this);
    });

这是我的测试indexcontroller。indeccontroller.test.js

describe('Index Controller', function() {
    var indexController;
    var commanServiceMock;
    var commanService;
    beforeEach(module('beatso.index-controller'));
    beforeEach(module(initMocks));
    beforeEach(inject(initIndexController));
    it('should return strong if password length is greater than equal to 8', function() {
        expect(indexController.checkPassword()).to.eql('strong');            
        expect(commanServiceMock.passwordValidator.calledOnce).to.eql(true);
    });

    function initMocks ($provide){

        commanServiceMock = {
            passwordValidator: sinon.spy()
        };
        $provide.service('commanService', function(){
            return commanServiceMock;
        })
    }
    function initIndexController($controller) {
        indexController = $controller('IndexController');
    }
}); 

这是我的常用服务

  'use strict';
    angular.module('beatso-comman.service', [])
        .factory('commanService', function(){
            var service = {
                passwordValidator: passwordValidator
            }
            function passwordValidator(password){
                if(password.length >= 8) {
                    return 'strong'
                }else {
                    return 'weak'
                }
            }
            return service;
        })
这是我对这项服务的测试。
 'use strict'
    describe('Test for my comman service', function(){
        var cService;
        beforeEach(module('beatso-comman.service'));
        beforeEach(inject(initCommanService));
        it('It should check the password strength', function(){
            expect(cService.passwordValidator('amtoverystrongpassword')).to.eql('strong');
        });
        function initCommanService(commanService){
            cService = commanService;
        }
    })

你的commandservice模拟没有方法"passwordValidator",所以尝试调用它会引发"undefined"错误。

如果你真的想测试你的服务,你不应该嘲笑它,而应该真正地测试它。你可以通过注入你的服务来获得对它的引用(参见Jasmine中的inject()函数)。

这是我的一个项目中的一段代码:

// inject the service itself 
beforeEach(inject(function(nfcService){
    service = nfcService;
}));

显然,"service"是我用来执行单元测试(并真正测试我的服务)的变量。

编辑-详细信息:我的意思是,你的控制器测试不应该测试你的服务……控制器的测试应该测试控制器。最终,它应该使用您的服务的模拟(对所需的方法进行监视),检查是否调用了适当的方法。

例如:

myServiceMock = {
    expectedMethod: jasmine.createSpy('expectedMethod spy')
}

在你的测试中:

expect(myServiceMock.expectedMethod).toHaveBeenCalled();

当使用$controller实例化控制器时,您可以(在第二个参数中)向它传递提供其依赖关系的对象字面值。这样,您就可以给它指定您想要的mock。

一个例子,仍然来自我的项目:

    menuCtrl = $controller('MenuController', {
        // where 'uiFeedbackService' is the name of the dependency
        'uiFeedbackService': uiFeedbackServiceMock
    });

注意:关于你的服务的声明,你可以直接return一个对象字面量,而不是创建一个变量,声明一个私有函数(passwordValidator),然后返回变量

angular.module('beatso-comman.service', [])
.factory('commanService', function(){
    return {
        passwordValidator: function(password){
            if(password.length >= 8) {
                return 'strong'
            }else {
                return 'weak'
            }
        }
    }
})
相关文章: