0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Promise.rejectがSpyOnできない!Spyしただけなのに実行されてしまう!

Last updated at Posted at 2020-03-06

Jasmine 3.3

Promiseでrejectしてエラー吐くようなパターンをテストしたい場合のお話ですが、
どうやら仕様?の様です(参照にGithubのissueのリンクあるのでよかったら見てね)

どうやらfakeAsyncの時の仕様みたい?

(完全には解決してない問題。誰か教えて!)

とりあえず下記二つは同じな様なので、適宜読み替えてください
(公式ドキュメントで見つけれてないので、違うかも)

promise.component.ts
Promise.reject('Something error occured!') ;

new Promise( function( resolve, reject ) {
	reject( "Something error occured!" ) ;
} ) ;

ダメな書き方(returnValueを使う)

test.component.spec.ts
spyOn(testService, 'func').and.returnValue(Promise.reject('Something error occured!'));

良い書き方(callFakeを使う)

test.component.spec.ts
spyOn(testService, 'func').and.callFake(() => {Promise.reject('Something error occured!'); });

returnValueだとその場で評価されてしまうみたい?(どうかと思うけど)

ちなみにfakeAsyncのなかだと、下記の様にキャッチするけど、Uncaught Promise Errorになるので、微妙にモヤモヤします。(上記で飛ばした'Something error occured!'だとマッチしない)

test.component.spec.ts
expect(() => {
  TestObj.func();
  tick();
}).toThrow();

参考

https://github.com/jasmine/jasmine/issues/1648
https://github.com/jasmine/jasmine/issues/1590

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?