[rspec-users] Dreading Controller Specs
baz at madeofstone.net
Tue Oct 21 08:08:23 EDT 2008
On 21 Oct 2008, at 10:45, Rob Lacey wrote:
> I almost am tempted to simplify the controller by using only the
> User model and moving most of the checks out of the controller
> action entirely and putting all into User, although that would mean
> that the user model, single_sign_on and music_service would then be
> really tightly coupled which wouldn't be great either. I
The "Rails Way" (assuming you are using Rails) is to make your
controllers do virtually nothing - find/create an object, call a
method on it, decide which view to render and that's it. So, actually
what you suggest would be best.
However, rather than talking to your models directly, the controller
could talk to a "presenter" object, which does the "glue
work" (finding the associated models, calling the relevant methods in
the correct order and packaging up the results) - you can then RSpec
your presenter in the same way as you would a model.
This makes your controller specs (and implementations) trivial:
it "should find a single sign-on" do
@presenter = mock 'SingleSignOnPresenter'
post :request_token, :secret => 'secret', :email_address => 'billg at hotmail.com
I actually use helpers (given_a_single_sign_on_presenter and
expect_to_request_a_token) instead of setting up the mocks and
expectations within the spec, just to make it a bit more readable.
Then you can RSpec your SingleSignOnPresenter separately, in much the
same way as you would spec a model, and keep the associations (and
implementation details) away from your controller.
Web design and development: http://www.3hv.co.uk/
Nottingham Forest: http://www.eighteensixtyfive.co.uk/
Serious Rails Hosting: http://www.brightbox.co.uk/
More information about the rspec-users