简体   繁体   中英

Multiple async test jasmine

Since previous week I have a issue with Jasmine test. I found from where comes the bug but I don't know how to resolve it.

Here the code :

it("Test the time of asynchronous call", function(done) {
        var isItDone = false;
        model.setSuccessFnc(function(isItDone) {
            isItDone = true;
            expect(isItDone).toBe(true);
            done();
        });
        model.setId("serviceLevel");
        model.getDataFromBatch("srService", "workItems", query.services, null, filter, null, true);

        setTimeout(function() {
            expect(isItDone).toBe(true);
            done();
        }, 4000);
    });

    it("Test retrieved data of asynchronous call", function(done) {
        // var isItDone = false;
        model.setSuccessFnc(function(oData, oResponse) {
            var json = JSON.parse(oData.__batchResponses[0].body);

            expect(json.d.results.length).toBeGreaterThan(0);
            done();

        });
        model.setId("serviceLevel");
        model.getDataFromBatch("srService", "workItems", query.services, null, filter, null, true);

    });

If I comment the first test, my second one work, if not I have this error :

Expected false to be true.

For me the second test use the first "done". How I can reset it?

PS : sorry for my engligh is not my native langage.

Thanks for your help.

several notes:

setTimeout in test is not the usual, jasmine has own timeout and expect in timeout is duplicate.

From sample is not evidently if you use real backend, or some mock. Use $httpBackend for mock responses and your tests will be faster.

First test contains error. Callback setSuccessFnc have parameter isItDone - it is actually local variable which "override" var isItDone defined at the beginning of the test.

If callback is called, local isItDone in callback is set to true and expectation in callback is fulfilled.

But second assertion in timeout work with var isItDone which is still false.

Result depends on what happens sooner.

This should help

model.setSuccessFnc(function() {
    isItDone = true;
    expect(isItDone).toBe(true);
    done();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM