我有一个主干集合,我需要将一些参数传递给 .fetch({data : {gender : 'male'}})。有没有办法用茉莉花测试参数是否通过?
提前致谢
我有一个主干集合,我需要将一些参数传递给 .fetch({data : {gender : 'male'}})。有没有办法用茉莉花测试参数是否通过?
提前致谢
您可能可以通过使用下面示例中的方法来实现这一点。 请注意,以下代码将拦截 fetch-call 并返回,调用不会到达服务器。如果您想拥有服务器端仿真,则需要使用 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.
});
});