Read more

Stub methods on any instance of a class in Rspec 1 and Rspec 2

Henning Koch
October 25, 2011Software engineer at makandra GmbH

RSpec 1 (Rails 2)

With the most recent spec_candy.rb helpers you can say:

User.stub_any_instance(:foo => :bar)
user =
# => :bar

RSpec 2 (Rails 3)

Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show snapshot

RSpec 2 comes with this feature built in:

User.any_instance.stub(:foo => :bar)
user =
# => :bar

RSpec 3

RSpec Mocks 3.3: any_instance Show snapshot

allow_any_instance_of(User).to receive(:foo).and_return(:bar)
user =
# => :bar
Posted by Henning Koch to makandra dev (2011-10-25 10:30)