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:
1 2 3 4 5 6 |
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 |
5 comments
1 ping
Skip to comment form ↓
Robby Russell
August 20, 2008 at 2:53 am (UTC -5) Link to this comment
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
Matt Williams
August 20, 2008 at 1:26 pm (UTC -5) Link to this comment
Robby,
Thanks for the response. Your tutorial has a lot of good ideas; I appreciate it.
srboisvert
September 16, 2008 at 12:16 pm (UTC -5) Link to this comment
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..
Julien
February 10, 2009 at 3:56 pm (UTC -5) Link to this comment
Awesome! Thanks Mike for thise. I will check Robby’s tutorial as well 😉
Jonathan Spooner
December 26, 2009 at 2:16 pm (UTC -5) Link to this comment
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.
Scott Motte » Blog Archive » Tips for using rspec for beginners
October 4, 2008 at 2:36 am (UTC -5) Link to this comment
[…] to rspec for restful_auth? – rspec, restful_authentication, and login_required – Also […]