有没有办法在RSpec取消存根?

时间:2013-04-23 17:37:03

标签: ruby rspec

搜索了Relish文档,但没有找到在RSpec中取消存储的方法。

这可能吗?

2 个答案:

答案 0 :(得分:94)

使用新的expect语法,不推荐使用unstub。你可以这样做:

# stub
allow(SomeClass).to receive(:a_method)

# do something...

# unstub
allow(SomeClass).to receive(:a_method).and_call_original

如果第一个allow包含.with或阻止,我相信它仍会继续拨打下一个电话,因此下一个allow不会清除这些的东西。

答案 1 :(得分:24)

rspec-mock代码表示您可以调用unstub方法。我引用:

  # Removes a stub. On a double, the object will no longer respond to
  # `message`. On a real object, the original method (if it exists) is
  # restored.
  #
  # This is rarely used, but can be useful when a stub is set up during a
  # shared `before` hook for the common case, but you want to replace it
  # for a special case.
  def unstub(message)
    ::RSpec::Mocks.space.proxy_for(self).remove_stub(message)
  end