我正在使用 Jasmine 和 Sinon 测试 Backbone.js 应用程序。我正在尝试验证单击按钮单击调用模型的 save() 方法并处理成功回调,该回调将消息添加到视图的 el 元素。我无法让 sinon 服务器触发模型的成功回调。
这是我的规范 beforeEach 的样子(beforeEach 中的变量都是 var 范围在 describe 函数中)。
beforeEach(function(){
server = sinon.fakeServer.create(); //create the fake server
server.respondWith([200, { "Content-Type": "text/html", "Content-Length": 2 }, "OK"]); //fake a 200 response
loadFixtures('signup_modal.html'); //load the fixture
element = $("#signupModal");
specSignUp = new SignUp();
signUpView = new SignUpView({model : specSignUp, el: $("#signupModal")});
});
这就是实际测试的样子:
it("Should call send request",function(){
element.find("#signupButton").trigger('click'); //click the button which should trigger save
server.respond(); //fake the response which should trigger the callback
expect(element).toContain("#message");
});
在尝试构建它的实现时,我创建了一个简单的回调方法来告诉我成功回调是被触发的:
sendRequest: function(){
console.log("saving");
this.model.save(this.model.toJSON(),{success: function(data){
console.log("success");
iris.addMessage(this.$("#messageContainer"),"Thank you");
}});
}
当我运行测试时,控制台显示“正在保存”,但没有调用成功回调。