Jasmineで非同期コードをテストするときに予期しない動作が発生しています。私たちが知る限り、done
関数を使用しているときは、doneが実行されるまで期待値は呼び出されません。しかし、それは第二期待が失敗しているので、それゆえ$ctrl.todos
割り当てはテストジャスミンで非同期関数をテストする
が起こったことはありませんが起こっていない
it('initializes the data when $onIinit', (done) => {
const expected = 'some result';
const response = Promise.resolve(expected);
spyOn(myService, 'getAll').and.returnValue(response);
// This method calls myService.getAll
$ctrl.$onInit();
expect(myService.getAll).toHaveBeenCalled();
expect($ctrl.todos).toEqual(false);
response.then(done);
});
出力:一方
偽等しくなるように、未定義の期待、これは動作しています:
it('initializes the data when $onIinit', (done) => {
const expected = 'some result';
const response = Promise.resolve(expected);
spyOn(myService, 'getAll').and.returnValue(response);
// This method calls myService.getAll
$ctrl.$onInit();
expect(myService.getAll).toHaveBeenCalled();
response
.then(() => expect($ctrl.todos).toBe(expected))
.then(done);
});
出力:テスト合格
コントローラ方法:
$ctrl.$onInit =() => {
myService.getAll().then((data) => {
$ctrl.todos = data;
});
};