I was look for a little advice really. I’ve been using RSpec for about 4
months now and I find it an absolute joy for model work and a really
nice tool it makes everything so much more readable and nicer to
However, I seem to dread spec-ing out controllers, they end up being
quite untidy, I think maybe I am approaching them in the wrong way as it
probably shouldn’t be as hard as I am making it.
For example I had a problem over the past few days where I am creating a
way of logging in a customer to our site through a token. There is a
UserController, the customer does a get to the action token_login, we
authenticate the token, check a few other things and then set a session
variable to keep them logged in.
it “should find a single sign on” do
mock_user(:generate_security_token => ‘newtoken’)
ms = mock_model(MusicService, :users => [mock_user])
sso = mock_model(SingleSignOn, :music_service => ms)
ms.users.stub!(:find_by_email).and_return(mock_user) post :request_token, :secret => @secret, :email_address =>
To be honest I feel very uncomfortable about the way this is arranged as
I am mocking several objects and their associations. I am trying to
stick to the “don’t touch the database” way of testing for controllers
and I feel that I am almost writing the implementation in the test just
by the way I am mocking / stubbing and therefore it makes the test
pointless as its just a confirmation of how I’ve written it.
Can anyone give me any ideas of how to do this properly?
How far do you go do you go with your mocking?
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 then don’t feel
comfortable because this approach would be as a reaction to making the
test simpler rather than making the controller code work which is the
whole point of the test.
Any help would be greatly appreciated. Cheers.