Luke Redpath
2005-Sep-11 23:59 UTC
Functional tests - post() to a specific controller (or get())
Hi there, I''m having some trouble with my functional tests - most of my controllers require a user to be logged in. My user login page is /user/login and the form on this page submits to /user/process_login (post). I''ve written my UserController functional tests no problem. I''ve extracted a common helper method that sends a post request to the above action, similar to the example in the Agile Rails book. However, even though I''ve put this helper method in my test_helper.rb file, it doesn''t work in any of my other functional tests because it tries to call the process_login action of whatever controller I''m testing, e.g. in my news_controller_test.rb testcase, if I call login() (the name of my helper method), it tries to post to NewsController#process_login which of course is wrong. I''ve looked through the Agile Rails book and the API docs and I can''t seem to find anything that tells you how you can set a specific controller using post() or get() (or presumably any of the other HTTP request methods). There must be a way of doing this, so could somebody please help me out? Cheers Luke _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Doug Alcorn
2005-Sep-12 01:13 UTC
Re: Functional tests - post() to a specific controller (or get())
"Luke Redpath" <luke-lJ1peEkt3K8wCQPON/xxHQmknBWnwzYrtUK59QYPAWc@public.gmane.org> writes:> However, even though I''ve put this helper method in my test_helper.rb > file, it doesn''t work in any of my other functional tests because it > tries to call the process_login action of whatever controller I''m > testing, e.g. in my news_controller_test.rb testcase, if I call login() > (the name of my helper method), it tries to post to > NewsController#process_login which of course is wrong.What I do is modify the session directly from my functional tests rather than calling my UserController login method. My authentication is based on the Login Generator. The wiki page documentation for that shows how to do functional testing with it. Basically you do something like this: @request.session[''user''] = @member in the functional test method. -- Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
Dan Sketcher
2005-Sep-12 02:25 UTC
Re: Functional tests - post() to a specific controller (or get())
Luke, For this, i use the following structure in my setup: # make sure we are authenticated @controller = Admin::LoginController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new login_as_admin @controller = Admin::TaxController.new So as you can see, I set up the controller for the login function, and then once logged in, change to the controller I want to test. Works a treat :) HTH, Dan