我用的是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] 删除。
我来说两句