Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Rails why can't I use reset_session or any of my methods in application_controller within my tests?

I tried to use reset_session in my test code but it complains no method found. I also tried to use some of the authentication methods ive written in my tests and they produced no method errors too (they are in my application_controller )

Can someone help to explain if

a) this is a problem experienced by all and not just me b) a workaround

*** EDIT - heres some code ********

from my app controller

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details
  include SpreedyTools
  # Scrub sensitive parameters from your log
  # filter_parameter_logging :password
  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

  #checks to see if there is a user logged in, if not redirects to login page.
  def login_required
    unless logged_in_user?
      flash[:error] = "You`ll need login first to access your account. Enter your details and we'll take you where you need to be."
      redirect_to :controller => 'account', :action => 'login'
      return
    end
  end
end

from my functional test

require 'test_helper'
require 'application_controller'

class DashboardControllerTest < ActionController::TestCase
  # Replace this with your real tests.
  setup :login_adam
  fixtures :users

  test "welcome page" do
    get :welcome
    assert_response :success
  end

  test "reset session works in my tests" do
     reset_session 
  end

  test "i can call my methods in application controller" do
    logged_in_user = logged_in_user?
  end

end

and errors when i run test:functionals

/usr/bin/ruby1.8 -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/functional/store_controller_test.rb" "test/functional/user_controller_test.rb" "test/functional/dashboard_controller_test.rb" "test/functional/account_controller_test.rb" 
Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.....E..E..............
Finished in 1.63359 seconds.

  1) Error:
test_i_can_call_my_methods_in_application_controller(DashboardControllerTest):
NoMethodError: undefined method `logged_in_user?' for #<DashboardControllerTest:0x7f18e162cd88>
    /test/functional/dashboard_controller_test.rb:31:in `test_i_can_call_my_methods_in_application_controller'

  2) Error:
test_reset_session_works_in_my_tests(DashboardControllerTest):
NameError: undefined local variable or method `reset_session' for #<DashboardControllerTest:0x7f18e162cc48>
    /test/functional/dashboard_controller_test.rb:27:in `test_reset_session_works_in_my_tests'

23 tests, 53 assertions, 0 failures, 2 errors
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -I"lib:test" "/usr/lib/ru...]
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:995:in `sh'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1010:in `call'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1010:in `sh'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1094:in `sh'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1029:in `ruby'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1094:in `ruby'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/testtask.rb:117:in `define'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1112:in `verbose'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/testtask.rb:102:in `define'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31
/usr/bin/rake:19:in `load'
/usr/bin/rake:19
like image 557
robodisco Avatar asked Nov 16 '09 12:11

robodisco


1 Answers

I think I found the answer to this:

@request.reset_session
like image 183
Cal Avatar answered Sep 26 '22 03:09

Cal