Error: Timeout - Async callback was not invoked within timeout specified… .DEFAULT_TIMEOUT_INTERVAL

自古美人都是妖i 提交于 2019-12-03 06:03:58

I would suggest that you replace setTimeout with $timeout so as to speed up your spec suite. You will need ngMock to be a part of your spec suite in order to get this working the intended way, but that seems to have already been taken care of looking at your spec. Good stuff.

Then in order to make the async nature of the spec "go away" you would call:

$timeout.flush([delay]) where delay is optional.

  • If no delay is passed, all the pending async tasks (inside the angular world) will finish what they're doing.
  • If a delay is passed, all pending tasks within the specified delay will finish. Those outside of the specified delay will remain in a 'pending' state.

With this, you can remove the done callback and write your tests as such:

describe('after being initialized', function () {
  var $timeout;

  beforeEach(function () {
    // Unable to get this working because the callback is never called
    userData.init();

    inject(function ($injector) {
      $timeout = $injector.get('$timeout');
    });
  }));

  it('should have a valid config', function () {
    $timeout.flush();
    // callback should've been called now that we flushed().
    expect(Object.keys(userData.getConfig()).length).toEqual(0);
  });
});

What Promise implementation are you using? I see a call to Promise.all but for the sake of continuing on with my answer I'm going to assume it is equivalent to $q.all. Running $timeout.flush should take care of resolving those values.

If you want to write expectations on the rejected/resolved values of a promise in Jasmine, I would look into something like jasmine-promise-matchers to make it clean and pretty, but barring that you could do something like this:

// $q
function get () {
  var p1 = $timeout(function () { return 'x'; }, 250);
  var p2 = $timeout(function () { return 'y'; }, 2500); 

  return $q.all([p1, p2]);
}

// expectation
it('is correct', function () {
  var res; 

  get().then(function (r) {
    res = r;
  });

  $timeout.flush(2500);

  expect(res).toEqual(['x', 'y']);
});

Depending on your setup, you may or may not have to stub out/spy on (depending on your frameworks definition of a spy) the promise in relation to your local config variable, but that's another story altogether I reckon.

I am not at all familiar with $firebase(something).$asObject.$loaded - as such I may have missed something here, but assuming it works 'just like any other promise' you should be good to go.

jsfiddle

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