简体   繁体   中英

Why is this async Jasmine test failing?

Why is this test failing, saying that the onSuccess spy was never called?

it('should correctly call the success callback',function(done)
{
    const callbacks={
        onSuccess:function()
        {
            console.log('OK');
            done();
        },
        onError:function()
        {
            console.log('ERR');
            done();
        }
    };

    spyOn(callbacks,'onSuccess').and.callThrough();
    spyOn(callbacks,'onError').and.callThrough();

    doSomethingAsync(callbacks.onSuccess,callbacks.onError);

    expect(callbacks.onSuccess).toHaveBeenCalled();
    expect(callbacks.onError).not.toHaveBeenCalled();
});

When running the test, I get an error saying Expected spy onSuccess to have been called. .
Right above that, there's a console log saying "OK", meaning that the spy was called and that it called through.

It's difficult to assert that one callback is called and another one isn't without stubbing doSomethingAsync or its internals.

Here's a contrived edge case:

function doSomethingAsync(onSuccess, onError) {
  setTimeout(function() {
    onSuccess('hello');
  }, 500);
  setTimeout(function() {
    onError(new Error('foo'));
  }, 1000);
}

(so it calls onSuccess after half a second, and onError after a second)

If you assert, in the onSuccess handler, that onError hasn't been called, the test will pass, even though onError is getting called (albeit half a second later).

This is something that you cannot easily work around, unless (as stated before) you stub (the internals of) doSomethingAsync .

If you simply want to test if doSomethingAsync calls the right handler, you can shorten your test case to this (provided that it's not strictly necessary to call the handlers in your callbacks object):

it('should correctly call the success callback',function(done) {
  doSomethingAsync(done, done.fail);
});

(this doesn't catch doSomethingAsync calling both handlers, though; if it calls onSuccess before onError , the test will pass).

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