[rspec-users] Authenticating before tests

Jarkko Laine jarkko at jlaine.net
Thu Sep 13 08:42:59 EDT 2007

On 13.9.2007, at 15.25, Luke Galea wrote:

> Hi all,
> I'm using Goldberg, an engine that provides roles based access
> control for my app. I need to login before I can do controller tests,
> but I can't find any examples of people doing this.
> Could someone point me in the right direction?

This is what we do:

in spec_helper.rb:

   include AuthenticatedTestHelper

   def login_as(user)
     controller.send :current_user=, user

Then, in a before(:all) block, I can say login_as(@user) with a user  
object. This assumes you're using restful_authentication (and should  
work with acts_as_authenticated as well).

> I thought the simplest way would be to either call the login action
> from my other tests before(:all), but I can't seem to find how to
> call another controller from within the spec for a different
> controller. (Results in @controller is nil).

You can't and shouldn't do that. Just find out how your particular  
authentication system determines the login and create a helper method  
like login_as above to simulate the login.

Jarkko Laine

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2417 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/rspec-users/attachments/20070913/2ce6788f/attachment.bin 

More information about the rspec-users mailing list