测试后清除SINON存根

发布时间:2022-07-27 / 作者:清心寡欲
本文介绍了测试后清除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

这篇关于测试后清除SINON存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持吉威生活!



[英文标题]Clear sinon stub after test


声明:本媒体部分图片、文章来源于网络,版权归原作者所有,如有侵权,请联系QQ:330946442删除。