Jasmine と Sinon を使用して Backbone.js アプリをテストしています。ボタンのクリックをクリックすると、モデルの save() メソッドが呼び出され、ビューの el 要素にメッセージを追加する成功のコールバックが処理されることを確認しようとしています。sinon サーバーがモデルの成功コールバックをトリガーするのに問題があります。
これは、私の仕様の beforeEach がどのように見えるかです (beforeEach の変数はすべて、describe 関数で var スコープです)。
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");
}});
}
テストを実行すると、コンソールに「保存中」と表示されますが、成功のコールバックが呼び出されません。