Reputation: 11
I am using sinon js's fake server with Jasmine for UT/IT. I have set the server.autorespond = true. (checked even after reducing the ms in server.autoRespondAfter)
Problem: Callbacks arenot triggered after the server has responded to the requests (I can check the logs of server requests and the server object itself to see the response texts). Jasmine fails the check for success or failure callback.CalledOnce.
This problem doesnot happen when: I was using server.respond() after setting server.respondWith(..) & my callbacks were triggered properly after server response. Jasmine passes the check for success or failure callback.CalledOnce.
My understanding is autorespond makes the server respond automatically as and when it gets the async requests and that includes calling the appropriate callbacks? Do I still need to use server.respond ?
Thanks.
code snippet: Read the required json file & send it as the server response. The json file read is a synchronous call (async false).
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
sinon.FakeXMLHttpRequest.useFilters = true;
sinon.FakeXMLHttpRequest.addFilter(function (method, url, async, username, password) {
// Don't fake json file read requests
if (url == inputUrl) {
return true;
}
});
this.resultfile = this.readJsonFile(inputUrl);
this.server.respondWith("GET", endUrl,
[200, { "Content-Type": "application/json" }, this.resultfile]);
console.log (this.server.requests); // Logs all requests so far
In my Jasmine spec I have
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();
Upvotes: 1
Views: 3244
Reputation: 2285
I know this is an old thread, but I thought I'd update for googlers:
The Sinon docs state that autoRespond
is not suitable for tests as it will execute asynchronously. This defaults to 10ms later, which is just enough to bump to the next frame of execution, potentially causing some race conditions with the tests. I discovered this because I had similar failing test cases related to AJAX.
I ended up implementing a respondImmediately
property on the fakeServer that will synchronously respond to any request. This just got merged into the project a few weeks ago (v1.14.0), but if you update to the newest version you should be able to get it. Check out the docs here.
Instead of setting this.server.autoRespond
property to true, set this.server.respondImmediately
property to true. At this point, there's no need for any this.server.respond()
calls.
Upvotes: 2
Reputation: 29083
jQuery.ajax is async... your test case will fail unless you wait long enough before calling expect(). alternatively, you can set make .ajax synchronous by doing jQuery.ajax({ url: '/abc', async: false, success: callbacks[0] });
though this won't work for certain types of requests (like cross domain)... see the jquery API docs for details on that
Upvotes: 0