Karma, Jasmine, Uncaught Error: <toHaveBeenCalledTimes> : Expected a spy, but got Function without test
See original GitHub issueSorry in advance for my bad english…
I am testing my application with Karma and Jasmine and I have a really strange error.
I have 9 tests and when I run tests it seems that karma finds a 10th test. And Karma returns me a Jamsine error : Uncaught Error: : Expected a spy, but got Function
That is strange is : when I launch my test in debug mode. All is good. More strange is that I have 3 files. Error occured just when I execute 3 files at the same times. With 2 files all is good ^^ …
Have you already seen this error?
I will give you two screens. First no debug mode. Second debug mode. And I will give you my code.
Two screens of my IDE (No Debug and Debug)
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();
})
});
});`
Thank you in advance ! 😃
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
@SegmentationPro @peteringram0 It will be great if you create a test project with this problem.
Because It looks like a problem of test code, not karma, karma-jasmine or jasmine-core
Thanks
I have stated getting this exact problem. My tests complete then at the end i get.