LoginSignup
1
0

More than 5 years have passed since last update.

AngularJS $timeout を Jasmine から実行した際にコールバックが実行されない問題

Posted at

下のコードだと$timeoutサービスのコールバックが実行さずにタイムアウトしてしまう。

appSpec.js
it('$timeout test', function () {
    var value = null;
    $timeout(function () {
        value = 'hoge';
    }, 100);

    expect(value).toBeNull();

    waitsFor(function () {
        return value !== null;
    }, 'timeout', 200);

    runs(function () {
        expect(value).toEqual('hoge');
    });
});

どうやら$timeout.flush(delay)をコールしてタイマーを進めると実行されるようになる模様。

appSpec.js

it('$timeout test', function () {
    var value = null;
    $timeout(function () {
        value = 'hoge';
    }, 100);

    expect(value).toBeNull();

    $timeout.flush();

    expect(value).toEqual('hoge');
});
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0