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

Mark Dodwell
Mon, 26 May 2008 19:52:43 -0000

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!?

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