Is there a way to unstub in RSpec?

Searched the Relish docs, but did not find a way to unstub in RSpec.

Is this possible?


Solution 1:

With new expect syntax, unstub is deprecated. You can do:

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

# do something...

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

If the first allow contains .with or a block, I believe it'll still carry to the next call, so the next allow doesn't clear those things.

Solution 2:

The rspec-mock code indicate that you can call the unstub method. I quote:

  # 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