How I provide a session object to Functional Tests?


#1

People,

I’m looking at p. 152 in the Rails book where he talks about testing
controllers [ functional tests ].

First he shows a method, test_login_with_valid_user, which tests
that it’s possible to login with a valid user/password combo.

Then, he talks about extracting the code he just wrote and placing it
into a login method inside of test_helper.rb

Once the method is there, he can re-use the login method in later
functional tests
of the LoginController class.

So, the discussion is easy to follow and I understand it.

My question:
Suppose I want to test other controllers besides the LoginController.
How do I make use of login in test_helper.rb to place the user object
in my session object?
Or in other words,
How do I make test_helper.login general enough to be used outside of
LoginController functional tests?

Thanks,

-Dan


#2

You can manipulate the session object as follows:

user = User.find(1)
@request.session[:user_id] = user.id

I just did the same last night when working out my own tests.

I hope this helps,

Michael