在Rails中为什么我不能在我的测试中使用reset_session或application_controller中的任何方法?

时间:2009-11-16 12:39:47

标签: ruby-on-rails testing

我尝试在我的测试代码中使用reset_session,但它抱怨找不到任何方法。 我还尝试使用我在测试中编写的一些身份验证方法,它们也没有产生方法错误(它们在我的application_controller中)

有人可以帮忙解释一下

a)这是所有人都遇到的问题,而不仅仅是我 b)解决方法

***编辑 - 下载一些代码********

来自我的app控制器

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

来自我的功能测试

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
运行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

2 个答案:

答案 0 :(得分:1)

我想我找到了答案:

@request.reset_session

答案 1 :(得分:0)

您必须明确定义要在视图/其他控制器中用作辅助方法的方法。所以对于所有那些你必须指定

helper_method :logged_in_user? 
helper_method :login_required

等 这将允许您在其他控制器/视图中访问它们