Any_instance with rspec


I really need to use any_instance - which is a Mocha feature with
rspec. I really can’t get around it. In “The Rails Way”, the author
says “It is possible to use Mocha together with RSpec…”. How?


Look in spec/spec_helper.rb, where you can tell RSpec which mock
framework you’d like to use. For reference, the one on my current
project looks like this:

== Mock Framework

RSpec uses it’s own mocking framework by default. If you prefer to

use mocha, flexmock or RR, uncomment the appropriate line:

config.mock_with :mocha

config.mock_with :flexmock

config.mock_with :rr


Hi Craig,

Thanks so much. The project was started last year and I didn’t even
have those lines in my spec_helper.rb. Serves me right for not reading
all the docs.


You’re welcome. Note that with each new release of RSpec, you should
re-run the rspec generation command. See: . Of course, before you do, you might
want to save a copy of spec/spec_helper.rb and spec/spec.opts so that
you can reapply your changes after they’re regenerated.


Cool. Thanks.

If you look at Ryan’s cast number 71 he uses any_instance to stub out
valid? on active record - this allows you to make sure, for example, to
just go to the behaviour of the controller and use stuff like
assigns[:fred].value == ‘whatever’.

I’m really struggling with overriding new and returning a prebuilt
object because I can’t check the low-level assign on a multi-model

Yeah, ok, I can say

assigns[:claim].should == @new_claim

But I wanted to check the fields in the claim and the address that
belongs to it are all being hit.

Can’t face learning mocha - no time right now.