测试后清除SINON存根 [英] Clear sinon stub after test

查看:20
本文介绍了测试后清除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存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆