This is partly for myself, and partly for anyone after me…..
I have a controller generated via rspec_scaffold — yes, I know, it might not be what all the cool kids are doing, but it works. I also have restful_authentication set up to use rspec. So, when I go to add before_filter :login_required, autotest frowns at me. After much trial and error, and googling (with false hits and things that didn’t work), I finally achieved a smiley face with the following added to my controller’s spec:
before :each do @current_user = mock_model(User, :id => 1) controller.stub!(:current_user).and_return(@current_user) controller.stub!(:login_required).and_return(:true) end fixtures :users




Matt,
Not sure if you know this, but most of your actions will require the login_required to be stubbed, you can move this into it’s own shared group for reuse in each spec group. I decided to post quick tutorial because I don’t see many people using it and/or mentioning it.
* http://rubyurl.com/OGdn
Robby,
Thanks for the response. Your tutorial has a lot of good ideas; I appreciate it.
Hi Matt. Just the question I was looking for the answer to. Timely.
BTW – your greater than symbol got translated to a HTML character entity and could mess up cut-an-pasters..
Awesome! Thanks Mike for thise. I will check Robby’s tutorial as well
You rock buddy! I’m moving all my unit test over to rspec and this post was a big help!
I had no idea the controller would be available from my test.