2012-04-11 56 views

回答

0

你也許可以在下面的例子中使用的方法實現這一目標。 請注意,,下面的代碼會攔截取回調用並返回,調用不會到達服務器。如果您想要進行服務器端模擬,那麼您需要使用Sinon或其他類似的方法。

describe("People collection" function() { 
     var people = Backbone.Collection.extend({ 
      // ... 
     }); 

     function searchPeople(people, data) { 
      people.fetch(data); 
     } 

     it("must verify the fetch parameters!", function(){ 
      var param = {data : {gender : 'male'}}; 
      // Set up the spy. 
      spyOn(people, 'fetch').andReturn(); // Warning: this makes the call synchronous, Fetch actually won't go through! 

      // Now perform the operation that would invoke Collection.fetch. 
      searchPeople(people, param); 

      expect(people.fetch).toHaveBeenCalled();   // Verifies the fetch was actually called. 
      expect(people.fetch).toHaveBeenCalledWith(param); // Verifies that the fetch was called with specified param. 

     }); 
    });