mocha - 如何用测试脚本证明,在Promise中reject之后并不会执行then的第一个函数
本文介绍了mocha - 如何用测试脚本证明,在Promise中reject之后并不会执行then的第一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
我是这样写的,但觉得不太严格,还有更好的写法吗?
it('抛出错误,将会跳过then的第一个函数的执行',function(done){
var result = 0;
var q = new Promise(function(resolve,reject){
throw new Error();
});
q.then(function(args){
result++;
},function(){
result++;
expect(result).to.be.equal(1);
done();
})
});
解决方案
下面这种,分别判断没有调用过、调用过一次。
it('抛出错误,将会跳过then的第一个函数的执行',function(done){
var q = new Promise(function(resolve,reject){
throw new Error();
});
const callbackThen = sinon.spy()
const callbackCatch = sinon.spy()
q.then(callbackThen, callbackCatch)
.done(function () {
expect(callbackThen.called).to.not.be.ok()
expect(callbackCatch.calledOnce).to.be.ok()
done()
})
});
这篇关于mocha - 如何用测试脚本证明,在Promise中reject之后并不会执行then的第一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文