我有一个返回承诺的方法。
doStuf() {
return somePromise
.then(...)
.catch(e => {...})
}
我想编写一个测试,确保该doStuff
方法返回的承诺具有一个 catch 方法。
怎么做 ?
我有一个返回承诺的方法。
doStuf() {
return somePromise
.then(...)
.catch(e => {...})
}
我想编写一个测试,确保该doStuff
方法返回的承诺具有一个 catch 方法。
怎么做 ?
这不是一个有用的测试。考虑到doStuf
以后可能会被重构为更多层的东西,其中可能存在多个链then
,catch
或者实现将以其他方式发生变化。
您实际上并不想测试实现,因为您可以简单地查看它以查看Promise 是否具有catch
. 不,你想测试. doStuf
您想要断言doStuf
返回一个承诺,并且该承诺返回给定特定输入的预期值。例如:
doStuf('foo').then(r => /* assert r equals expected outcome */)
doStuf('bar').catch(r => /* assert r equals expected error */)