48

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

这可能吗?

4

2 回答 2

121

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

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

# do something...

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

如果第一个allow包含.with或块,我相信它仍然会进行到下一个调用,所以下一个allow不会清除这些东西。

于 2014-07-30T00:51:55.120 回答
26

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
于 2013-04-23T18:18:42.657 回答