Normally in my Rails controller tests, I'll do before { Website.any_instance.stub(:save).and_return(false) }
to test what happens when the record does not save. It looks like any_instance
went away with Rspec 3.
I tried using before { allow(Website).to receive(:save).and_return(false) }
for Rspec 3, but now I get this error:
Website(id: integer, ...) does not implement: save
Is there a replacement for the very useful any_instance
with Rspec 3?
Use allow_any_instance_of(Class).to receive when you want to configure how. instances of the given class respond to a message without setting an. expectation that the message will be received.
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.
let generates a method whose return value is memoized after the first call. This is known as lazy loading because the value is not loaded into memory until the method is called. Here is an example of how let is used within an RSpec test. let will generate a method called thing which returns a new instance of Thing .
Try this
allow_any_instance_of(Website).to receive(:save).and_return(false)
Eg: https://github.com/rspec/rspec-mocks#settings-mocks-or-stubs-on-any-instance-of-a-class
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