Jasmine Sinon JS假服务器自动响应使用

Jasmine Sinon JS假服务器自动响应使用,jasmine,sinon,Jasmine,Sinon,我正在使用sinon js的假冒服务器和Jasmine进行UT/IT。我已经设置了server.autorespond=true。(即使在减少服务器中的ms.autoRespondAfter后仍检查) 问题:在服务器响应请求后不会触发回调(我可以检查服务器请求日志和服务器对象本身以查看响应文本)。Jasmine未能通过成功或失败回调的检查。CalledOnce 在以下情况下不会发生此问题:我在设置server.respondWith(..)后使用server.respond()&我的回调在服务器

我正在使用sinon js的假冒服务器和Jasmine进行UT/IT。我已经设置了server.autorespond=true。(即使在减少服务器中的ms.autoRespondAfter后仍检查)

问题:在服务器响应请求后不会触发回调(我可以检查服务器请求日志和服务器对象本身以查看响应文本)。Jasmine未能通过成功或失败回调的检查。CalledOnce

在以下情况下不会发生此问题:我在设置server.respondWith(..)后使用server.respond()&我的回调在服务器响应后正确触发。Jasmine通过了成功或失败callback.CalledOnce检查

我的理解是autorespond使服务器在收到异步请求时自动响应,这包括调用适当的回调?我还需要使用server.respond吗

谢谢

代码片段:读取所需的json文件并将其作为服务器响应发送。读取的json文件是一个同步调用(async false)

在我的茉莉花中我有

var callbacks = [sinon.spy(),sinon.spy()];

// call the above sinon code and then make the test call below

jQuery.ajax({
     url: '/abc',
     success: callbacks[0]
});

// this fails, though I can see the server responded to the request.
expect(callbacks[0].calledOnce).toBeTruthy();

ajax是异步的。。。除非在调用expect()之前等待足够长的时间,否则测试用例将失败。或者,您可以通过执行
jQuery.ajax({url:'/abc',async:false,success:callbacks[0]})来设置make.ajax synchronous虽然这对某些类型的请求(如跨域)不起作用。。。请参阅jquery API文档以了解有关该线程的详细信息

我知道这是一个旧线程,但我想我应该为Google更新:

Sinon文档指出,
autoRespond
不适用于测试,因为它将异步执行。这默认为10毫秒后,这刚好足以跳到下一个执行帧,可能会导致测试出现一些争用情况。我发现这一点是因为我有类似的与AJAX相关的失败测试用例

最后,我在fakeServer上实现了一个
respondInstance
属性,该属性将同步响应任何请求。这在几周前(v1.14.0)刚刚被合并到项目中,但是如果您更新到最新版本,您应该能够得到它


不要将
this.server.autoRespond
属性设置为true,而是将
this.server.respondInstance
属性设置为true。此时,不需要任何
this.server.respond()
调用。

对我来说很好。。。张贴一些code@RobertLevy谢谢你的回复。我已经用代码更新了Q。如果我使请求同步,它就会通过。但如果我将它作为异步调用,并在jasmine中使用waitsFor,它无论如何都会失败<代码>预期(回调[0].calledOnce.toBeTruthy();->false expect(回调[1]。calledOnce.toBeFalsy();->true
这样就不会调用任何回调(成功或失败)。循环sinon js调用,sinon-1.3.4.js中的以下函数将为json文件读取返回true,为实际异步测试调用返回false。不确定为什么我的回调不在集合中<代码>函数some(collection,callback){for(var index=0;index
var callbacks = [sinon.spy(),sinon.spy()];

// call the above sinon code and then make the test call below

jQuery.ajax({
     url: '/abc',
     success: callbacks[0]
});

// this fails, though I can see the server responded to the request.
expect(callbacks[0].calledOnce).toBeTruthy();