Posted by Tom Rossi on December 22, 2006
Misc
Okay, so for some reason I can't post with Google Groups to the Ruby on Rails forum, so I'll just post here on our blog. This is something that took me a little while to dig up and could save some poor Rails programmer some time.
Many times in our functional controllers we will need to set attributes of the request before testing. This is done pretty simply as follows:
# Within a functional test
@request.cookies['my_cool_variable'] = CGI::Cookie.new('my_cool_variable', 'coolness')
@request.host = 'www.my_url.com'
get :index
assert_response :success
But how do you set request variables with an integration test? Integration tests are much more powerful than functional tests because they allow you to not only test multiple controllers, but you can create any number of sessions. In all the excitement, there is
plenty of documentation on how to set the request attributes for multiple sessions, but not when you want to just test one session. The missing piece is the
@integration_session object. This is the default session object that all of your test methods operate on when you do your
get or
post etc.
# Within an integration test
@integration_session = open_session # This is only necessary if the object is not yet instantiated
@integration_session.cookies['my_cool_variable'] = CGI::Cookie.new('my_cool_variable', 'coolness')
@integration_session.host = 'www.my_url.com'
get '/controller/index'
assert_response :success
Why would you do this? Only for simple integration tests where you will not have multiple sessions and you want to make all of your calls to your test methods directly without referencing a specific session.
Give us a piece of your mind.
No comments thus far - add yours