Stubbing authentication in request spec

A request spec is a thin wrapper around ActionDispatch::IntegrationTest, which doesn’t work like controller specs (which wrap ActionController::TestCase). Even though there is a session method available, I don’t think it is supported (i.e. it’s probably there because a module that gets included for other utilities also includes that method).

I’d recommend logging in by posting to whatever action you use to authenticate users. If you make the password ‘password’ (for example) for all the User factories, then you can do something like this:

def login(user)
  post login_path, :login => user.login, :password => 'password'
end

Leave a Comment