已经在 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);
});
这是我对 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;
}
})