# Any\_instance with rspec

**URL:** <https://www.ruby-forum.com/t/any-instance-with-rspec/138205>\
**Category:** Rails\
**Created:** [May 21, 2008, 2:10am UTC](https://www.ruby-forum.com/t/any-instance-with-rspec/138205 "2008-05-21T02:10:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [May 21, 2008, 2:10am UTC](https://www.ruby-forum.com/t/any-instance-with-rspec/138205/1 "2008-05-21T02:10:06Z")

</div>

Hi,

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?

Thanks,  
Tiberiu

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [May 21, 2008, 3:17am UTC](https://www.ruby-forum.com/t/any-instance-with-rspec/138205/2 "2008-05-21T03:17:25Z")

</div>

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

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [May 21, 2008, 5:35am UTC](https://www.ruby-forum.com/t/any-instance-with-rspec/138205/3 "2008-05-21T05:35:36Z")

</div>

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.

Tiberiu

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [May 21, 2008, 5:43am UTC](https://www.ruby-forum.com/t/any-instance-with-rspec/138205/4 "2008-05-21T05:43:12Z")

</div>

You’re welcome. Note that with each new release of RSpec, you should  
re-run the rspec generation command. See:  
[http://rspec.info/upgrade.html](http://rspec.info/upgrade.html) . 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.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [May 21, 2008, 7:07pm UTC](https://www.ruby-forum.com/t/any-instance-with-rspec/138205/5 "2008-05-21T19:07:07Z")

</div>

Cool. Thanks.

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [November 18, 2008, 4:09pm UTC](https://www.ruby-forum.com/t/any-instance-with-rspec/138205/6 "2008-11-18T16:09:13Z")

</div>

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  
controller.

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.
