简体   繁体   中英

How to test a promise has a catch method defined?

I have a method that returns a promise.

doStuf() { 
  return somePromise
    .then(...)
    .catch(e => {...}) 
}

I want to write a test that makes sure the promise returned by the doStuff method has a catch method.

How to do that?

That is not a useful thing to test. Consider that doStuf may be refactored later into something more multi-layered, where several then - catch chains may exist, or the implementation will change in some other ways.

You don't actually want to test the implementation , because you can simply look at it to see whether the promise has a catch . No, you want to test the behaviour of doStuf . You want to assert that doStuf returns a promise, and that that promise returns the expected value given certain input. Eg:

doStuf('foo').then(r => /* assert r equals expected outcome */)
doStuf('bar').catch(r => /* assert r equals expected error */)

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