[英]Angular $q promise Unit testing using Mocha, Chai, Sinon
[英]Unit testing MOCHA SINON CHAI (check call nested functions)
如果有人了解测试,请告诉我如何实现两件事的测试:
1)在makeRing函数启动时调用了obj.newRing方法。 2)参数'num'是否传递给函数makeRing( num )是否与在obj.newRing({ number:num })中传递的对象的属性匹配。
function makeRing (num) {
currRing = obj.newRing ({number: num});
}
也许有人会对如何使用sinon有一些想法,否则在这种情况下,我将很高兴收到任何信息。 我受了很长时间...谢谢!
如果您可以在测试中访问obj
,则可以执行以下操作:
// create a spy for your function:
const newRingSpy = sinon.spy();
// replace the real function with the spy:
sinon.stub(obj, 'newRing', newRingSpy);
// run the test:
makeRing(7);
// 1) validate that obj.newRing was called exactly once:
expect(newRingSpy.calledOnce).to.be(true);
// 2) and/or validate the arguments it was called with:
expect(newRingSpy.firstCall.args).to.eql([{number: 7}]);
如果您只想知道是否完全调用了该函数,则第二项检查已将其覆盖(如果未调用该函数,则newRingSpy.firstCall
为null)。
如果您无权访问obj
,那么将生产代码更改为以下内容可能是最好的策略:
function makeRing (num, obj) {
currRing = obj.newRing ({number: num});
}
然后,您可以在测试中轻松将存根的obj
传递给makeRing()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.