Sarah Vessels
Sarah Vessels

Reputation: 31630

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?

Upvotes: 19

Views: 6716

Answers (1)

usha
usha

Reputation: 29349

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

Upvotes: 39

Related Questions