jasmine.js expect() does not work inside an asynchronous callback

匆匆过客 提交于 2019-12-10 12:30:02

问题


I'm getting acquainted with Jasmine (http://pivotal.github.com/jasmine/) and found something rather baffling:

it("should be able to send a Ghost Request", function() {
  var api = fm.api_wrapper;

  api.sendGhostRequest(function(response) {
    console.dir('server says: ', response);
  });

  expect(true).toEqual(false);
});

Fails as expected.

However, moving the expect call inside the callback:

it("should be able to send a Ghost Request", function() {
  var api = fm.api_wrapper;

  api.sendGhostRequest(function(response) {
    console.dir('server says: ', response);
    expect(true).toEqual(false);
  });
});

Somehow passes :O

After some debugging: api.sendGhostRequest() does an asynchronous ajax request, and jasmine rushes past before the request has completed.

Hence the question:

How do I get jasmine to wait for ajax execution before ascertaining the test result?


回答1:


Edit for Jasmine 2

Asynchronous tests have become much simpler in Jasmine 2. Any test that needs to handle asynchronous code can be written with a callback which will indicate the completion of the test. See the Jasmine 2 docs under the header Asynchronous Support

it('should be able to send a ghost request', (done) => {
    api.sendGhostRequest((response) => {
        console.log(`Server says ${response}`);
        expect(true).toEqual(false);
        done();
    });
});

Jasmine 1

Have a look at waitsFor() and runs() on the Jasmine site under the header Asynchronous Support.

Use of runs and waitsfor should force Jasmine to wait for the ajax call to finish or for some timeout.

The code would look like:

it("should be able to send a Ghost Request", function() {
    runs(function() {
        api.sendGhostRequest(function(response) {
            console.dir('server says: ', response);
            flag = true;
        });
    }, 500);

    waitsFor(function() {
        return flag;
    }, "Flag should be set", 750);

    runs(function() {
        expect(true).toEqual(false);
    });
}

In which case the expect would fail.




回答2:


As @pkopac commented, runs() and waitsFor() have been deprecated in v2 favour of using a done() callback as documented: https://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support

it("should be able to send a Ghost Request", function(done) {
    var api = fm.api_wrapper;

    var success = function(response) {
        console.dir('server says: ', response);
        expect(response).toEqual('test response')
        done();
    };

    api.sendGhostRequest(success);
});



回答3:


Look into runs() and waitfor()

Specifically you can call waitfor to check that the callback has run in some fashion (maybe using a boolean as a check?) and then run the expect afterwards.

runs allows you to wait until the waitfor has completed.

async jasmine documentation



来源:https://stackoverflow.com/questions/15141834/jasmine-js-expect-does-not-work-inside-an-asynchronous-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!