如何检查sinon在Promise中调用的函数

水晶

我用的是mocha+ chai+sinon测试功能,但在使用的承诺called财产,即使写一个未定义的函数,called总是true,这是什么造成的?如何解决?THX

describe("when click get verifycode", function () {
it('if get server response success, should start countdown', function (done) {
    // given
    sandbox.stub(model, 'getVerifyCode').resolves({});
    sandbox.spy(view, 'startCountDown');
    // when
    var result = instance.onClickVerify('123456');
    // then
    result.then(res => {
        // no matter what function, called always true
        // e.g. expect(AnUndefinedFunction.called).to.be.ok;
        expect(instance.view.startCountDown.called).to.be.ok;
        done();
    }).catch(err => {
        done();
    });
特里斯坦·赫塞尔

这里的问题是该.catch块正在捕获块中发生的任何错误.then

这是一个问题,因为您在done没有传递err对象的情况下调用Mocha 将此解释为测试正确通过,而不是失败。


修复:

describe("when click get verifycode", function () {
    it('if get server response success, should start countdown', function (done) {
        // given
        sandbox.stub(model, 'getVerifyCode').resolves({});
        sandbox.spy(view, 'startCountDown');
        // when
        var result = instance.onClickVerify('123456');
        // then
        result.then(res => {
            // no matter what function, called always true
            // e.g. expect(AnUndefinedFunction.called).to.be.ok;
            expect(instance.view.startCountDown.called).to.be.ok;
            done();
        }).catch(err => {
            done(err);
        });
    })
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章