【问题标题】:What is the difference between sinon stub.restore and stub.resetsinon stub.restore 和 stub.reset 有什么区别
【发布时间】:2020-09-28 16:53:53
【问题描述】:

清理时,用 sinon 创建 stub 后,可以恢复或重置它。有人可以向我解释这两者之间有什么区别吗?或者什么时候用restore,什么时候用reset?

【问题讨论】:

    标签: javascript testing sinon


    【解决方案1】:

    当你有以下代码时:

    const stub = sinon.stub(object, "foo");
    

    原来的object.foo 方法不见了。如果您想恢复它,您可以使用object.foo.restore() 或简单地使用stub.restore() 作为快捷方式。换句话说,restoreobject 有影响。

    stub.reset() 会影响stub 本身,重置其行为和历史。 (缓存数据,比如它被调用了多少次,以前的调用......)

    【讨论】:

      猜你喜欢
      • 2017-07-06
      • 2010-10-02
      • 2011-12-12
      • 2010-09-16
      • 2012-03-14
      • 2012-02-06
      • 2011-02-25
      • 2011-11-22
      • 2015-03-26
      相关资源
      最近更新 更多