gpt4 book ai didi

javascript - Angular Testing 类型错误

转载 作者:行者123 更新时间:2023-12-03 08:40:03 26 4
gpt4 key购买 nike

已经尝试过 karma、Mocha chai 和 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);
});

这是我对索引 Controller 的测试。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;
}
})

最佳答案

您的commanService模拟没有方法“passwordValidator”,因此尝试调用它会引发“未定义”错误。

如果你确实想测试你的服务,你不应该 mock 它,而应该真正测试它。您可以通过注入(inject)来获取对您的服务的引用(请参阅 Jasmine 中的 insert() 函数)。

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

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

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

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

例如:

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

在你的测试中:

expect(myServiceMock.expectedMethod).toHaveBeenCalled();

当使用 $controller 实例化 Controller 时,您可以(在第二个参数中)向其传递一个提供其依赖项的对象文字。这样,您就可以给它您想要的模拟。

仍然来 self 的项目的示例:

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

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

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

关于javascript - Angular Testing 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33064950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com