# this works fine (which it should)
def test_truth
assert true
end
def test_get_index
login_as(:someone) # 'someone' is set up in the users_fixture yaml
file
get :index
assert_response :success
end
end
The test_get_index function fails. It can't access the current_user
object. It tries to check the authentication, which I have set up in the
ApplicationController and it says nil.check_authentication is failing,
and it should be current_user.check_authentication.
Anyone know how to get current_user to work within functional tests?
Well, I couldn't find a solution to this, so I decided to rip out all
the current_<user_model_name> from authenticated_systems module and add
it to the application controller and made a similar function in
application helper. This did the trick, so now any tests I run will
work.
module AuthenticatedTestHelper
# Sets the current user in the session from the user fixtures.
def login_as(user)
@request.session[:user_id] = user ? users(user).id : nil
end
def authorize_as(user)
@request.env["HTTP_AUTHORIZATION"] = user ?
ActionController::HttpAuthentication::Basic.encode_credentials(users(user).email,
'test') : nil
end
end
in my functionals tests, I only write (as an example..)
login_as(:xxx) xxx is the fixture name of a user.....