ole*_*tar 9 javascript jasmine sinon
我在Jasmine中为Backbone应用程序编写单元测试.当然,我在测试中使用了Sinon.但现在我有问题.我正在为登录屏幕编写测试,我需要模拟服务器响应 - 因为服务器工作非常糟糕.现在我的代码看起来:
describe('Login', function(){
it('Should simulate server response', function(){
server = sinon.fakeServer.create();
server.respondWith("GET", "http:\\example.com", [200, {"Content-Type": "application/json"}, '{"Body:""asd"}'])
})
$('body').find('button#login').trigger('click');
server.respond();
server.restore()
console.log(server.requests);
})
Run Code Online (Sandbox Code Playgroud)
这段代码运行正常,但是我在控制台中看到假冒所有请求,但在登录期间我也有其他请求,我不需要使用假服务器.这是下一个屏幕的要求.也许存在对特殊请求进行过滤或使用虚假响应的方法.请帮帮我.谢谢.
And*_*rle 12
诀窍是在服务器的FakeXMLHttpRequest对象上使用过滤器.然后,只有您过滤掉的请求才会使用虚假服务器:
server = sinon.fakeServer.create();
server.xhr.useFilters = true;
server.xhr.addFilter(function(method, url) {
//whenever the this returns true the request will not faked
return !url.match(/example.com/);
});
server.respondWith("GET", "http:\\example.com", [200, {"Content-Type": "application/json"}, '{"Body:""asd"}'])
Run Code Online (Sandbox Code Playgroud)