Node.js Sinon存根未正确恢复

Node.js Sinon存根未正确恢复,node.js,mocha.js,sinon,Node.js,Mocha.js,Sinon,我正在使用mocha、chai和sinon测试我的node express代码 我遇到了一个奇怪的问题,看起来sinon无法恢复存根,因此在下一次测试中,我得到了beknown错误 Attempted to wrap <method1> which is already wrapped 还有一个 step('should do other stuff', test(function () { const stub1 = sinon.stub(my_model1,

我正在使用mocha、chai和sinon测试我的node express代码

我遇到了一个奇怪的问题,看起来sinon无法恢复存根,因此在下一次测试中,我得到了beknown错误

Attempted to wrap <method1> which is already wrapped
还有一个

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();
        });
});
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();
        });
});