# Spec testing controllers

**URL:** <https://www.ruby-forum.com/t/spec-testing-controllers/176685>\
**Category:** Rails\
**Created:** [October 4, 2009, 11:07pm UTC](https://www.ruby-forum.com/t/spec-testing-controllers/176685 "2009-10-04T23:07:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [October 4, 2009, 11:07pm UTC](https://www.ruby-forum.com/t/spec-testing-controllers/176685/1 "2009-10-04T23:07:16Z")

</div>

I just started using rspec and mocha to test all of my controllers.  
Before I was using factory girl to test do my testing, but all those  
database hits really take a long time. However i’m finding i’m spending  
way more time writing stubs and mocks to satisfy my controllers than I  
am actually writing tests.

This is a two part general question

1. Am i doing something completely wrong in my controllers? I’m testing  
with integrate\_views and the number of times something.stubs(:each)  
comes up way to often in my specs. Most of my controllers involve at  
least 2 normally 3 models.

2. Is there a way to auto generate stubs and mocks like factory-girl, so  
i don’t have to worry about populating every last property of every last  
mock\_model’d ? I can just call Mock\_Factory.create(:website) and set up  
a list of defaults somewhere else?

I feel like my stubs and mocks are supposed to help me, not confuse me:

[http://pastie.org/641647](http://pastie.org/641647)

Your thoughts?

---

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [October 7, 2009, 6:40pm UTC](https://www.ruby-forum.com/t/spec-testing-controllers/176685/2 "2009-10-07T18:40:29Z")

</div>

Thanks for the reply James! These certainly look promising and merit  
digging into further. Looks like others have the same idea, but there is  
no cohesive standard/incredibly-stable&proven way to do this.

---

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [October 5, 2009, 1:55pm UTC](https://www.ruby-forum.com/t/spec-testing-controllers/176685/3 "2009-10-05T13:55:59Z")

</div>

2009/10/4 Richard S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> comes up way to often in my specs. Most of my controllers involve at  
> [http://pastie.org/641647](http://pastie.org/641647)
> 
> Your thoughts?

Hi Richard,

I haven’t used it myself, but you might find this thread [1] of  
interest. In particular, Murray S.'s fork [2] of factory\_girl  
sounds a bit like what you are looking for.

Alternatively, David C.'s Stubble [3] might offer an  
alternative approach, although I think that currently requires a small  
patch to Mocha.

Regards, James.

> **[James Mead](https://jamesmead.org/)**
>
> Ruby Developer, author of Mocha, and ex-Antarctic Hero.

[1] [http://rubyurl.com/23WA](http://rubyurl.com/23WA)  
[2] [GitHub - h-lame/factory\_bot: Fork of factory\_bot from way back in 2008 to add after\_build and after\_create callbacks before the main gem had these features. Keeping this for posterity because the commit wasn't included directly in the main repo.](http://github.com/h-lame/factory_girl/tree/master)  
[3] [GitHub - dchelimsky/stubble: Tools to make stubbing Ruby ORM models just a little bit easier](http://github.com/dchelimsky/stubble)

---

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [October 7, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/spec-testing-controllers/176685/4 "2009-10-07T19:21:29Z")

</div>

Richard S. wrote:

> I just started using rspec and mocha to test all of my controllers.  
> Before I was using factory girl to test do my testing, but all those  
> database hits really take a long time.

Are you sure that’s where you’re losing time? (For the record, I’m  
using and loving Machinist.)

> However i’m finding i’m spending  
> way more time writing stubs and mocks to satisfy my controllers than I  
> am actually writing tests.

Then that’s a sign that something is wrong…perhaps those DB hits  
aren’t so bad! 🙂

> This is a two part general question
> 
> 1. Am i doing something completely wrong in my controllers? I’m testing  
> with integrate\_views and the number of times something.stubs(:each)  
> comes up way to often in my specs. Most of my controllers involve at  
> least 2 normally 3 models.
> 
> 2. Is there a way to auto generate stubs and mocks like factory-girl, so  
> i don’t have to worry about populating every last property of every last  
> mock\_model’d ? I can just call Mock\_Factory.create(:website) and set up  
> a list of defaults somewhere else?

The answer to both of these questions is: go back to using factories.  
If you don’t like Factory Girl, try Machinist.

Also, you might want to think about writing fewer controller specs and  
using Cucumber more…

> I feel like my stubs and mocks are supposed to help me, not confuse me:
> 
> [http://pastie.org/641647](http://pastie.org/641647)

Yeah, that lookls like the kind of stuff I used to write before  
Machinist…

> Your thoughts?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
