Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the new equivalent of Rspec any_instance in Rspec 3?

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?

like image 612
Sarah Vessels Avatar asked Jun 30 '14 20:06

Sarah Vessels


People also ask

What is Allow_any_instance_of?

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.

What is stubbing in RSpec?

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.

What is let in RSpec?

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 .


1 Answers

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

like image 101
usha Avatar answered Sep 23 '22 02:09

usha