my “login” controller has a method “authenticate” which needs
params[:username] and params[:password]. Please help!
You can pass several hashes to all of the request methods: params,
session and flash. From the AWDR chapter on Testing, specifically
Testing Controllers:
my “login” controller has a method “authenticate” which needs
params[:username] and params[:password]. Please help!
You can pass several hashes to all of the request methods: params,
session and flash. From the AWDR chapter on Testing, specifically
Testing Controllers:
But if i am in my ActivitiesController functional test, then a call
to :
post :login, :user => {:name => “fred”, :password => “abracadabra”}
will post to the login action of ActivitiesController and not
LoginController. Do you see my problem?
Well, yes and no. Obviously, if you aren’t in the LoginController,
you don’t want to be sending a post to one of its actions.
But, in order to test your ActivitiesController you need to be an
already logged in user when you call the action. As long as your
LoginController sets one, or more, values in the session object that
signifies you are logged in, then you don’t really have a problem.
Simply pass those same values as part of the get call:
get :index, {}, {:logged_in => true}
Where the second hash that you pass to get/post/put/delete/head
contains the values you want to seed into the session object…
my “login” controller has a method “authenticate” which needs
params[:username] and params[:password]. Please help!
Thanks
Allen
Since you’re going to be testing the actual login process in the
functional tests for your login controller, you might as well just
bypass it elsewhere.
In the setup method, you can say: @request.session[:user] = User.new :name => ‘Superhacker’
(or whatever the particulars of your login system require.)
Hi,
You can pass an object as a third parameter that contains the data that
would be in you session.
Since I’m using activeRbac all I need is a rbac_user with a couple roles
to get past my login system.
fixtures :users, :roles
def test_index
get :index, {}, {:rbac_user => get_user}
assert_response :success
assert_template ‘index’
end
private
def get_user
user = users(:testuser001)
user.roles.push roles(:Admin)
user.roles.push roles(:User)
end