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

David Chelimsky
Mon, 26 May 2008 20:05:48 -0000

On May 26, 2008, at 2:52 PM, 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.
>
> Does anybody have any suggestions on how to avoid this or perhaps I am
> doing something wrong!?

Use stub_model instead (if you''re using source from github - if not
the 1.4 release is coming soon).

>
>
> I make use of a Factory pattern to create instances for my model unit
> tests -- could I use a similar pattern for a ''mock
factory''? Is
> anybody
> doing anything similar?
>
> ~ Mark
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users