I utilize QUnit in combination with sinon. Is there a way to make sinon's fakeserver respond to multiple chained ajax calls triggered from the same method?
module('demo', {
beforeEach: function(){
this.server = sinon.fakeServer.create();
},
afterEach: function(){
this.server.restore();
}
});
test('chained ajax calls', function(assert){
this.server.respondWith('GET', '/foo', [200,
{ 'Content-Type': 'application/json' }, '{ "foo": 1 }' ]);
this.server.respondWith('GET', '/bar', [200,
{ 'Content-Type': 'application/json' }, '{ "bar": 1 }' ]);
var successCount = 0;
$.get('/foo', function(data){
successCount++;
$.get('/bar', function(){
console.log('bar success');
successCount++;
});
});
this.server.respond();
assert.strictEqual(successCount, 2);
});
The issue I am facing is that only one of the methods returns a response. Can the fakeserver handle this situation effectively?
Update: It appears that adding another server.respond()
resolves the problem. However, is there a more efficient approach available?
Based on the documentation, it suggests that one call should suffice, regardless of the number of ajax calls:
server.respond(); Triggers responses for all pending asynchronous requests.
Check out the Fiddle here: http://jsfiddle.net/3qj20r5m/1/