Hi I have some protected methods in my application controller to handle
logging in users and Id like to be able to use them in my tests. But
whenever I try to run my tests i always get errors E.g.
NameError: undefined local variable or method `logged_in_user'' for
#<DashboardControllerTest:0x7f32bb483cb8>
how do i get round this. Heres some code -
class ApplicationController < ActionController::Base
protected
def logged_in_user?
@logged_in_user = User.find(session[:user]) if session[:user]
end
def logged_in_user=user
if !user.nil?
session[:user] = user
@logged_in_user = user
end
end
def logged_in_user
if logged_in_user?
return @logged_in_user
end
end
end
and my testfile
require ''test_helper''
require ''application_controller''
class DashboardControllerTest < ActionController::TestCase
fixtures :users
test "welcome page" do
get :welcome
assert_response :success
end
test "logged in user can access dashboard" do
assert logged_in_user, users(:valid_user)
get :welcome
assert_response :success
end
end
--
Posted via http://www.ruby-forum.com/.
Nicholas Henry
2009-Nov-17 15:27 UTC
Re: how to use methods in application_controller in tests
As the method is in another class the test case does not have access to it. From looking at your test case it does not appear that you are logging in a user. I would check out the test helpers in restful_authentication[1] and use login_as for example. If you which to test if a user is logged in then you will assert that that @request.session[:user] is not nil. Personally I would not do this assertion in this test as you are testing if a logged in user can access the dashboard, not if a user is logged in. [1] http://github.com/technoweenie/restful-authentication/blob/classic/generators/authenticated/templates/authenticated_test_helper.rb --- Nicholas Henry Ruby on Rails Developer nicholas-avpPrnUtcvTqJdGyAr2GoA@public.gmane.org Firsthand Web Design and Development p 514.949.6869 Website http://www.firsthand.ca Twitter http://twitter.com/firsthand Blog http://blog.firsthand.ca/ Fork me on Github http://github.com/firsthand On Nov 14, 1:23 am, Adam Akhtar <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi I have some protected methods in my application controller to handle > logging in users and Id like to be able to use them in my tests. But > whenever I try to run my tests i always get errors E.g. > > NameError: undefined local variable or method `logged_in_user'' for > #<DashboardControllerTest:0x7f32bb483cb8> > > how do i get round this. Heres some code - > > class ApplicationController < ActionController::Base > protected > > def logged_in_user? > @logged_in_user = User.find(session[:user]) if session[:user] > end > > def logged_in_user=user > if !user.nil? > session[:user] = user > @logged_in_user = user > end > end > > def logged_in_user > if logged_in_user? > return @logged_in_user > end > end > end > > and my testfile > > require ''test_helper'' > require ''application_controller'' > > class DashboardControllerTest < ActionController::TestCase > fixtures :users > > test "welcome page" do > get :welcome > assert_response :success > end > > test "logged in user can access dashboard" do > assert logged_in_user, users(:valid_user) > get :welcome > assert_response :success > end > end > -- > Posted viahttp://www.ruby-forum.com/.