mardi 26 mai 2015

How to test this async server response with Jasmine?

I want to write a Jasmine test for an async fetch call. This is how my test looks at the moment:

describe("async fetch call", function(){
        it ("should contact server and call callback success", function(){
            var server = sinon.fakeServer.create();
            server.autoRespond = true;
            server.respondWith([200, {'Content-Type': 'application/json'}, '{"collection":"coll","response":"resp", "options":"opts", "readyState":"1"}']);
            var callback = {
                success: jasmine.createSpy("success").and.callFake(function (){
                    expect(callback.success).toHaveBeenCalled();
                }),
                error: jasmine.createSpy("error")
            };
            session.getCompanies(callback);
            server.restore();
        });
    });

The function session.getCompanies is calling the fetch command. I use Jasmine 2.2.0 and SinonJS. When I run this test, expect is called too late. How can I make the test wait until expect was called?

Aucun commentaire:

Enregistrer un commentaire