# Advantage of using mock\_model() over stub()

**URL:** <https://www.ruby-forum.com/t/advantage-of-using-mock-model-over-stub/170222>\
**Category:** Rails\
**Created:** [June 14, 2009, 11:59am UTC](https://www.ruby-forum.com/t/advantage-of-using-mock-model-over-stub/170222 "2009-06-14T11:59:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![CrankyMonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@CrankyMonkey](https://www.ruby-forum.com/u/CrankyMonkey)\
**Post date:** [June 14, 2009, 11:59am UTC](https://www.ruby-forum.com/t/advantage-of-using-mock-model-over-stub/170222/1 "2009-06-14T11:59:42Z")

</div>

What’s the advantage of using mock\_model() over stub() for a view  
spec? It seems like you would want to use the stub() if you are using  
a outside-in methodology since you probably haven’t even generated  
your model yet.

---

<div class="post-metadata">

**Author:** ![CrankyMonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@CrankyMonkey](https://www.ruby-forum.com/u/CrankyMonkey)\
**Post date:** [June 14, 2009, 3:34pm UTC](https://www.ruby-forum.com/t/advantage-of-using-mock-model-over-stub/170222/2 "2009-06-14T15:34:43Z")

</div>

With mock\_model you’ll get a mock-object that automatically has an  
unique ‘id’ defined, a ‘to\_params’ method, and a ‘new\_record?’ method  
(that returns false). Here’s an example of the difference between the  
two:

@user = mock(‘User’, :id =\> 10, :new\_record? =\> false, :login =\>  
‘nicholas’)

vs.

@user = mock\_model(User, :login =\> ‘nicholas’)

Mock\_model is a convenience, and as you point out in a view-spec where  
you might not have the model created yet it might make sense to go  
without. However, you’ll see on the RSpec examples (rspec.info/rails/  
writing/views.html) that they show them using mock\_model. I think  
it’s your preference. It isn’t difficult to generate an empty model  
class as a placeholder for the future while you’re writing your view.  
If I know I’m showing user information on a view, then I can probably  
assume I’ll be utilizing a User model of some type.

Remember with a outside-in approach you want to go in and out. Dive  
in, come up for air, rinse and repeat. It isn’t that you want to bang  
out your entire view in one session, then your controller then model.  
You want to work on smaller slices and iterate and increment.

-Nicholas
