[rspec-users] Mocking Models in Controller Specs...

Bart Zonneveld
Tue, 27 May 2008 07:16:13 -0000

On 26-mei-2008, at 21:52, Mark Dodwell wrote:

> I find myself doing this kind of thing a lot in Controller Specs:
>
>       @vacancy = mock_model(Vacancy)
>       @vacancy.stub!(:reference)
>       @vacancy.stub!(:title)
>       @vacancy.stub!(:created_at)
>       @vacancy.stub!(:updated_at)
>       @vacancy.stub!(:body)
>       @vacancy.stub!(:contract)
>       @vacancy.stub!(:location)
>       @vacancy.stub!(:salary)
>       @vacancy.stub!(:benefits)
>       @vacancy.stub!(:start_date)
>       @vacancy.stub!(:duration)
>
> I have to stub each of these methods so that the form renders
> correctly
> (as I am using integrate_views). This just feels *way* too much
> code and
> effort to do this. I have a spec for one controller (which just does
> plain old CRUD) that is over 300 LOC -- this seems a bit nuts to me
> when
> the controller is only 50 LOC.

Or, consider moving code into the model, and test it there. See
http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model

gr,
bartz