2012-07-18 1 views
1

Sinon을 사용하여 가짜 서버를 만드는 방법을 발견했습니다.
다음은 코드 (1), (2)입니다.가짜 서버를 Sinon에서 Jasmine으로 변환하는 방법.

Jasmine을 사용하여 똑같은 것을 만들 수 있습니까?
예인 경우. 코드 (1)과 (2)를 어떻게 다시 작성해야합니까?


(1)

 beforeEach(function() { 
      this.server = sinon.fakeServer.create(); 
      this.server.respondWith(
       'GET', 
       Routing.generate('api_get_url') + '/' + this.model.get('id'), 
       JSON.stringify(this.fixtureResponse) 
      ); 
     }); 

(2)

 it('should the response not change', function() { 
      this.model.fetch(); 
      this.server.respond(); 
      expect(this.fixtureResponse).toEqual(this.model.attributes); 
     }); 

답변

1

이 코드가 서버에 액세스하는 방법에 따라 달라집니다하지만 만약 사용 jQuery의 $.ajax 또는 $.get (또는 비슷한 방식으로 중앙 집중화 된 것) 백본이하는 것처럼, 당신은 그것을 스텁 (stub)하고 위조 된 응답을 되돌릴 수 있습니다. # 1 그래서 커피 스크립트에서, 같은 약 보일 것이다 또한

spyOn($,'get').andCallFake (options) => 
    if options.url == Routing.generate('api_get_url') + '/' + @model.get('id') 
    options.success(JSON.stringify @fixtureResponse) 

참조 : Preventing AJAX call with Jasmine and Sinon using Backbone