我正在使用的單頁應用程序有兩種形式的登錄視圖:登錄表單和註冊表單。以下規範描述了這些表單的測試。我使用Jasmine-jQuery 1.4.2.茉莉花測試使用.toHaveBeenCalledWith註冊表單失敗
// 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
});
});
});
});
測試的登錄表單運行正常,但測試的註冊表單失敗。
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)
在應用程序中使用表單沒有問題。數據被傳輸。一切正常。只是測試沒有。
解決方法
然而,該測試是成功當我延遲執行。
_.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();
});
如果您刪除signInForm測試,會發生什麼情況?或者在signUpForm測試後移動它? – 2013-05-24 19:14:22
也許別的什麼是停止事件,然後在一些行動後重新觸發。看起來有可能在表單提交中不應刷新頁面。如果塞子不同步觸發,那麼這個間諜將失敗。 – fncomp 2013-05-25 02:12:20