How to delaying specific response from fake backend in Sinon JS?

448 views
Skip to first unread message

Андрей Волосович

unread,
Jul 12, 2017, 5:14:31 AM7/12/17
to Sinon.JS
Hello

In my App I use - Backbone framework. Karma, Jasmine, SinonJS - for testing.

I have simple Backbone view, like this:

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
        });

    });
});

This test is delaying all responses, but I need delaying only for one.
When I have first request (collection1) before testAsync and second (collection2) in testAsync - it's not working. I received only data from collection1.
Another way, it's use option server.autoRespondAfter = ms, but in this case I have delay for all responses. I need only for one of them.
Thanks for your help.

Андрей Волосович

unread,
Jul 14, 2017, 4:55:14 AM7/14/17
to Sinon.JS
Hello guys,
My solution on the stackoverflow 
Reply all
Reply to author
Forward
0 new messages