提前抱歉我的英文不好..
我正在用Karma和Jasmine测试我的应用程序,我有一个非常奇怪的错误。
我有9个测试,当我进行测试时,业力似乎找到了第10个测试。而Karma给我一个Jamsine错误:
未捕获错误::预计有间谍,但有功能
奇怪的是:当我在调试模式下启动测试时。一切都很好。
更奇怪的是我有3个文件。我在同一时间执行3个文件时发生错误。有2个文件都很好^^ ...
您是否已经看到此错误?
我会给你两个屏幕。首先没有调试模式。第二种调试模式。我会告诉你我的代码。
activate.controller.js
describe('userActivateCtrl', function() {
beforeEach(module('Betizy'));
var $controller;
beforeEach(inject(function(_$controller_, _ngToast_, _$filter_, _userService_, _navigationService_){
$controller = _$controller_;
ngToast = _ngToast_;
$filter = _$filter_;
userService = _userService_;
navigationService = _navigationService_;
}));
describe('at its launch', function () {
it('add an info toast in the toastList to say at the user what to do.', function () {
var controller = $controller('userActivateCtrl', {
});
expect(ngToast.messages.length).toBe(1);
expect(ngToast.messages[0].className).toBe('info');
expect(ngToast.messages[0].content).toBe($filter('translate')('user.activate.message'));
})
});
describe('for a valid registration', function () {
it("call method from userService when activation is called by the click on activation button.", function () {
var fakeHttpPromise = {
then: function () {}
};
var controller = $controller('userActivateCtrl', {
});
controller.userActivateForm = {$valid: true};
spyOn(userService, 'activate').and.returnValue(fakeHttpPromise);
spyOn(navigationService, 'toIndex').and.returnValue(fakeHttpPromise);
controller.activate();
expect(userService.activate).toHaveBeenCalledTimes(1);
setTimeout(function () {
expect(navigationService.toIndex).toHaveBeenCalledTimes(1);
done();
})
})
});
describe('for an invalid activation form', function () {
it("does nothing.", function () {
var controller = $controller('userActivateCtrl', {
});
var fakeHttpPromise = {
then: function () {}
};
controller.userActivateForm = {$valid: false};
spyOn(userService, 'activate').and.returnValue(fakeHttpPromise);
ngToast.dismiss();
controller.activate();
expect(ngToast.messages.length).toBe(0);
expect(userService.activate).not.toHaveBeenCalled();
})
});
});
的 login.controller.js
describe('userLoginCtrl', function() {
beforeEach(module('Betizy'));
beforeEach(inject(function (_$controller_, _$stateParams_, _ngToast_, _$filter_) {
$controller = _$controller_;
$stateParams = _$stateParams_;
ngToast = _ngToast_;
$filter = _$filter_;
}));
describe('at its launch', function () {
it('add an info toast in the toastList to say at the user what to do.', function () {
var controller = $controller('userLoginCtrl', {});
expect(ngToast.messages.length).toBe(1);
expect(ngToast.messages[0].className).toBe('info');
expect(ngToast.messages[0].content).toBe($filter('translate')('user.login.message'));
});
it('add an danger toast in the toastList if user was mistaken during authentication.', function () {
$stateParams.state = "error";
var controller = $controller('userLoginCtrl', {});
expect(ngToast.messages.length).toBe(2);
expect(ngToast.messages[0].className).toBe('danger');
expect(ngToast.messages[0].content).toBe($filter('translate')('user.login.error'));
});
});
});
的 register.controller.js
describe('userRegisterCtrl', function(){
beforeEach(module('Betizy'));
var $controller;
beforeEach(inject(function(_$controller_, _userService_, _ngToast_, _$filter_, _navigationService_){
$controller = _$controller_;
userService = _userService_;
ngToast = _ngToast_;
$filter = _$filter_;
navigationService = _navigationService_;
}));
describe('at its launch', function () {
it('add an info toast in the toastList to say at the user what to do.', function () {
var controller = $controller('userRegisterCtrl', {
});
expect(ngToast.messages.length).toBe(1);
expect(ngToast.messages[0].className).toBe('info');
expect(ngToast.messages[0].content).toBe($filter('translate')('user.register.message'));
})
});
describe('for a valid registration', function () {
it("call method from userService when register is called by the click on register button.", function () {
var fakeHttpPromise = {
then: function () {}
};
var controller = $controller('userRegisterCtrl', {
});
controller.userRegisterForm = {$valid: true};
spyOn(userService, 'register').and.returnValue(fakeHttpPromise);
spyOn(navigationService, 'toIndex').and.returnValue(fakeHttpPromise);
controller.register();
expect(userService.register).toHaveBeenCalledTimes(1);
setTimeout(function () {
expect(navigationService.toIndex).toHaveBeenCalledTimes(1);
done();
})
})
});
describe('for an invalid registration with invalid mail adress', function () {
it("add a danger toast in the toastList for an incorrect mail adress pattern.", function () {
var controller = $controller('userRegisterCtrl', {
});
controller.userRegisterForm = {$error: {email: true}, $valid: false};
ngToast.dismiss();
controller.register();
expect(ngToast.messages.length).toBe(1);
expect(ngToast.messages[0].className).toBe('danger');
expect(ngToast.messages[0].content).toBe($filter('translate')('user.register.error.email.pattern'));
})
});
describe('for an invalid registration and a valid mail adress', function () {
it("does nothing.", function () {
var controller = $controller('userRegisterCtrl', {
});
var fakeHttpPromise = {
then: function () {}
};
controller.userRegisterForm = {$error: {email: false}, $valid: false};
spyOn(userService, 'register').and.returnValue(fakeHttpPromise);
ngToast.dismiss();
controller.register();
expect(ngToast.messages.length).toBe(0);
expect(userService.register).not.toHaveBeenCalled();
})
});
});
先谢谢你的帮助,我会变得疯狂!
编辑:1 当我使用检查器调试chrome时,我看到:
成功userActivateCtrl,用于通过点击激活按钮调用激活时来自userService的有效注册调用方法。
在我看到之后:
未捕捉错误::期待间谍,但得到了功能。 用法:expect()。toHaveBeenCalledTimes() at compare(jasmine.js:3302)at Expectation.toHaveBeenCalledTimes(jasmine.js:1543) 在activate.controller.js:41
所以问题是为什么它执行那个,而测试刚刚过去?
谢谢你的帮助:)