测试后清除SINON存根

人气:1,003 发布:2022-10-16 标签: javascript node.js stub sinon

问题描述

我的一个测试中有此存根:

sinon.stub(service, 'batchNote')
    .resolves(mResponse);

测试后能不能把它清理掉?如果是,如何?

推荐答案

是的,有可能。

SinonAPI有restore方法用于stubs。来自docs

调用object.method.restore();(或stub.restore())

即可恢复原函数

所以使用您的示例,您可以简单地执行以下操作:

const stub = sinon.stub(service, 'batchNote');
stub.resolves(mResponse);

console.log(service.batchNote()); // outputs stubbed value

stub.restore()
console.log(service.batchNote()); // outputs original

398