我正在开发的单页应用程序有一个登录视图,其中包含两个表单:登录表单和注册表单。以下规范描述了这些表单的测试。我正在使用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 = "firstname.name@email.com";
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 = "firstname.name@email.com";
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
});
});
});
});
登录表单的测试运行成功,但注册表单的测试失败。
Error: Expected spy FormSubmitSpy to have been called with \
[ { name : 'John Doe', email : 'firstname.name@email.com', \
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)
使用应用程序中的表单没有问题。数据已传输。一切都运作良好。只是测试不行。
解决方法
然而,当我延迟其执行时,测试是成功的。
_.defer(function() {
expect(callback).toHaveBeenCalledWith({
name: name,
email: email,
password: password,
password_confirmation: password
});
});
为什么这个方法能够成功而“正常”的实现失败了?
这是对给定情况的简化:
it("should evaluate true", function() {
var foo = false;
_.defer(function() {
foo = true;
});
expect(foo).toBeTruthy();
});