The single page application I am working on has a login view with two forms: a sign-in form and a sign-up form. The following spec describes tests for these forms. I am using Jasmine-jQuery 1.4.2.
// user_spec.js
describe("User", function() {
var userController;
beforeEach(function () {
loadFixtures('menu.html');
userController = new MyApp.User.Controller();
});
describe("LoginView", function() {
beforeEach(function() {
// Mock the $.ajax function to prevent XHR:
spyOn($, "ajax").andCallFake(function(params) {});
});
it("should pass email and password with the 'signInForm:submit' event.", function() {
var email = "[email protected]";
var password = "Passw0rd";
var callback = jasmine.createSpy("FormSubmitSpy");
userController.loginView.$el.find("#signInEmail").val(email);
userController.loginView.$el.find("#signInPassword").val(password);
userController.loginView.bind("signInForm:submit", callback, this);
userController.loginView.ui.signInForm.trigger("submit");
expect(callback).toHaveBeenCalledWith({
email: email,
password: password
});
});
it("should pass name, email and password with the 'signUpForm:submit' event.", function() {
var name = "John Doe";
var email = "[email protected]";
var password = "Passw0rd";
var callback = jasmine.createSpy("FormSubmitSpy");
userController.loginView.$el.find("#signUpName").val(name);
userController.loginView.$el.find("#signUpMail").val(email);
userController.loginView.$el.find("#signUpPassword").val(password);
userController.loginView.$el.find("#signUpPasswordConfirmation").val(password);
userController.loginView.bind("signUpForm:submit", callback, this);
userController.loginView.ui.signUpForm.trigger("submit");
expect(callback).toHaveBeenCalledWith({
name: name,
email: email,
password: password,
password_confirmation: password
});
});
});
});
The test for the sign-in form runs successful however the test for the sign-up form fails.
Error: Expected spy FormSubmitSpy to have been called with \
[ { name : 'John Doe', email : '[email protected]', \
password : 'Passw0rd', password_confirmation : 'Passw0rd' } ] \
but it was never called.
at new jasmine.ExpectationResult (http://localhost:3000/assets/jasmine.js?body=1:114:32)
at null.toHaveBeenCalledWith (http://localhost:3000/assets/jasmine.js?body=1:1235:29)
at null.<anonymous> (http://localhost:3000/assets/user_spec.js?body=1:233:24)
at jasmine.Block.execute (http://localhost:3000/assets/jasmine.js?body=1:1064:17)
at jasmine.Queue.next_ (http://localhost:3000/assets/jasmine.js?body=1:2096:31)
at jasmine.Queue.start (http://localhost:3000/assets/jasmine.js?body=1:2049:8)
at jasmine.Spec.execute (http://localhost:3000/assets/jasmine.js?body=1:2376:14)
at jasmine.Queue.next_ (http://localhost:3000/assets/jasmine.js?body=1:2096:31)
at jasmine.Queue.start (http://localhost:3000/assets/jasmine.js?body=1:2049:8)
at jasmine.Suite.execute (http://localhost:3000/assets/jasmine.js?body=1:2521:14)
Using the forms in the application there is no problem. Data is transmitted. Everything works fine. Just the test does not.
Workaround
The test however is successful when I delay its execution.
_.defer(function() {
expect(callback).toHaveBeenCalledWith({
name: name,
email: email,
password: password,
password_confirmation: password
});
});
Why does this work and the "normal" implementation fails?
Here is a simplification of the given case:
it("should evaluate true", function() {
var foo = false;
_.defer(function() {
foo = true;
});
expect(foo).toBeTruthy();
});