我正在使用mocha,chai和sinon来测试我的节点表达代码。
我遇到一个奇怪的问题,看起来sinon无法恢复存根,因此在下一个测试中,我得到了众所周知的错误
Attempted to wrap <method1> which is already wrapped
这是我的工作
mocha-steps
代替it()
子句,以便它们按顺序运行(我想确保它不是异步竞争条件)
这是一个测试用例:
step('should do stuff', test(function () {
const stub1 = sinon.stub(my_model1, 'method1');
chai.request(server)
.get('/endpoint')
.send(body)
.end(function (err, res) {
stub1.restore();
do_various_assertions(err, res);
});
}));
还有一个
step('should do other stuff', test(function () {
const stub1 = sinon.stub(my_model1, 'method1');
chai.request(server)
.get('/endpoint')
.send(slightly_different_body)
.end(function (err, res) {
stub1.restore();
do_various_assertions(err, res);
});
}));
我在上面的错误
Attempted to wrap <method1> which is already wrapped
如果我在第二种情况下将存根注释掉,则效果很好。但为什么?我究竟做错了什么?
下一步应该知道上一步已经完成,需要调用done
函数。在您的示例中,第二步不等待第一步,method1
也不会还原。
step('should do stuff', function (done) {
const stub1 = sinon.stub(my_model1, 'method1');
chai.request(server)
.get('/endpoint')
.send(body)
.end(function (err, res) {
stub1.restore();
do_various_assertions(err, res);
done();
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句