Searched the Relish docs, but did not find a way to unstub in RSpec.
Is this possible?
In RSpec, a stub is often called a Method Stub, it's a special type of method that “stands in” for an existing method, or for a method that doesn't even exist yet. Here is the code from the section on RSpec Doubles − class ClassRoom def initialize(students) @students = students End def list_student_names @students.
Use the allow method with the receive matcher on a test double or a real. object to tell the object to return a value (or values) in response to a given. message. Nothing happens if the message is never received.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With