0
votes

I am fetching the data using the backbone fetch call. When I tried to test it using the jasmine, I got an error as Error: Timeout - Async callback was not invoked within the timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

When I console the valueReturned it provides me the actual value which I need. There is no error in the response. But the only thing which I get on my page is timeout error which I specified above.

Can you please let me know what I am doing wrong?

describe("fetch call", function() {
var valueReturned;
beforeEach(function(done) {
    window.jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
    setTimeout(function () {
    todoCollectionCursor.fetch({
        success : function(collection, response, options) {
            valueReturned = options.xhr.responseText; 
            return valueReturned;
        }
    });
    },500);
});

it("should return string", function(done) {
       console.log(valueReturned);
       expect(typeof valueReturned).toEqual('string');
});
});
2

2 Answers

0
votes

Try this:

describe("fetch call", function() {

jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;

var valueReturned;

beforeEach(function(done) {

    setTimeout(function () {
    todoCollectionCursor.fetch({
        success : function(collection, response, options) {
            valueReturned = options.xhr.responseText; 
            return valueReturned;
        }
    });
    },500);
});

it("should return string", function(done) {
       console.log(valueReturned);
       expect(typeof valueReturned).toEqual('string');
});
});

Also, I'm not sure if it's intended, but instead of beforeEach, I would you beforeAll as in this example it will fit better as there is no setup or any initialization

If you code is not working, try this setup, it works for me, and update your code accordingly

describe("Testing app: ", function () {

    jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000;

    beforeAll(function (done) {
        setTimeout(done, 10000);
    });



    it("Verify: true is true with 10 sec delay ", function (done) {
        setTimeout(true.toBe(true), 10000);
        done();
    });
});
0
votes

I found the answer for it. Since it is an Ajax call I should mock the request. This I found under jasmine-Ajax - https://github.com/jasmine/jasmine-ajax

it("Response check response when you need it", function() {

              var doneFn = jasmine.createSpy("success");
              $.ajax({
                 url: '/fetch',
                 method: 'GET',
                 success : function(data)
                 {
                     doneFn(data);
                 }
              })
          jasmine.Ajax.requests.mostRecent().respondWith({
                "status": 200,
                "contentType": 'text/plain',
                "responseText": '[{title:"Aravinth"}]'
           });
          expect(jasmine.Ajax.requests.mostRecent().responseText).toEqual('[{title:"Aravinth"}]');
   });