首页 > 如何用测试脚本证明,在Promise中reject之后并不会执行then的第一个函数

如何用测试脚本证明,在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()
         })
    });
【热门文章】
【热门文章】