2012-02-25 5 views
1

Jasmine과 Sinon을 사용하여 Backbone.js 앱을 테스트하고 있습니다. 버튼 클릭을 클릭하면 Model의 save() 메서드가 호출되고 뷰의 el 요소에 메시지를 추가하는 success 콜백을 처리하는지 확인하려고합니다. 모델의 성공 콜백을 트리거하기 위해 sinon 서버를 가져 오는 데 문제가 있습니다.Backbone.js 테스트 Sinon을 사용하여 저장 성공 콜백을 호출하지 않음

Heres 전에 beforeEach의 변수는 before입니다. 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")}); 
}); 

그리고이 같은 실제 시험이 모습입니다 : 이것의 구현을 구축을 위해 노력하는 동안 내가 성공 콜백이 트리거 beign 것을 나에게 보여주기 위해 간단한 콜백 메소드를 생성

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"); 
    }}); 
} 

콘솔을 실행하면 "저장"되지만 성공 콜백이 호출되지 않습니다.

+2

오류 콜백이 호출 되었습니까? – abraham

+0

아 하! 오류가 발생했습니다! 나는 그걸 찾지 않을 거라 생각 했어. – bittersweetryan

답변

4

server.respondWith() 메서드에서 응답 텍스트가 유효한 텍스트이기 때문에 백본은 응답 텍스트가 유효한 JSON이고 응답이 "OK"이기 때문에 폭탄을 터뜨릴 것으로 예상합니다.

로 변경 방법 :

server.respondWith([200, {"Content-Type":"text/html","Content-Length":2}, '{"OK":"True"}']); 

성공 콜백은 성공적으로 처리되고 있었다.

+0

그것은 총 낭비가 아니 었습니다. @abraham의 발언은 나를 올바른 길로 인도했습니다. 그것없이 나는 결과를 빨리 얻었을 것이라고 생각하지 않는다. 하지만 그래, 그 항상 작은 것들이 나중에 얻을! – bittersweetryan

관련 문제