View = Backbone.View.extend({
el: ‘#app’,
initialize: function () {
var collection1 = new Collection1();// creates collection which can receive data from backend
var collection2 = new Collection2(); // another one
collection1.fetch().done(function() { // backbone sends the request for data to backend
//render data1 which received from backend and append to el
});
collection2.fetch().done(function() {
//render data2 which received from backend and append to el
});
}
});
Two Collections send different requests (different urls).
I need to test this code, and I need make situation when collection2 will return data from backend after collection1. This situation needs to be guaranteed. It’s a main idea of this test.
I know how to delay all request to fake backend, but I don’t understand how to delay one of them.
I use async test from this JSFiddle
My code for test:
define(function(require) {
//get all dependencies
function testAsync(delayTime) { // make delay
var deferred = $.Deferred();
setTimeout(function() {
deferred.resolve();
}, delayTime);
return deferred.promise();
}
describe('test ', function() {
var view = null,
server = null;
beforeEach(function(done) {
server = sinon.fakeServer.create(); // fake sinon server
server.autoRespond = true; // enable autoanswer
testAsync(3000) // delay 3 sec
.done(function(done) {
server.respondWith('GET', urlsForRequest.collection1, [
200,
{"Content-Type": "application/json"},
mockData1]);
server.respondWith('GET', urlsForRequest.collection2, [
200,
{"Content-Type": "application/json"},
mockData2]);
done(); // all is good go to it section, it's a jasmine func
});
view = new View(); // create Backbone View
});
afterEach(function() {
server.restore();
});
it('can be instantiated', function() {
// in this section need to use expect
});
});
});