2012-07-18 45 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(或類似的東西集中)Backbone的方式,你可以存根並返回假響應。因此,#1看起來大致是這樣的,在CoffeeScript中:

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