Ramblings

19 Aug, 2008

rspec, restful_authentication, and login_required

Posted by: Matt Williams In: rails| tips

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

5 Responses to "rspec, restful_authentication, and login_required"

1 | Robby Russell

August 20th, 2008 at 2:53 am

Avatar

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

2 | Matt Williams

August 20th, 2008 at 1:26 pm

Avatar

Robby,
Thanks for the response. Your tutorial has a lot of good ideas; I appreciate it.

3 | srboisvert

September 16th, 2008 at 12:16 pm

Avatar

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

4 | Scott Motte » Blog Archive » Tips for using rspec for beginners

October 4th, 2008 at 2:36 am

Avatar

[...] to rspec for restful_auth? - rspec, restful_authentication, and login_required - Also [...]

5 | Julien

February 10th, 2009 at 3:56 pm

Avatar

Awesome! Thanks Mike for thise. I will check Robby’s tutorial as well ;)

Comment Form

Categories

DrakNet Web Hosting

Flickr PhotoStream

    layout_newm3headerTerrain Testa

About

Matt Williams is a geekly jack of all trades residing in Columbus, OH, USA.

Recommend Me


Adopt a Dragon from Dragcave
Adopt one today!Adopt one today!Adopt one today!Adopt one today! Adopt one today! Adopt one today!Adopt one today! Adopt one today! Adopt one today! Adopt one today!