ruby-on-rails – Rails 4.0 RuntimeError:@controller为nil:确保在测试的设置方法中设置它

当我运行控制器(或功能)测试时,即使在Rails 3.2中运行相同的测试,Rails 4.0也无法自动实例化控制器实例(@controller).

有关如何开始解决此问题的任何建议?

样本输出:

 $ruby -Ilib:test test/controllers/account_controller_test.rb
[Coveralls] Set up the SimpleCov formatter.
[Coveralls] Using SimpleCov's 'rails' settings.
warning: parser/current is loading parser/ruby21, which recognizes
warning: 2.1.5-compliant syntax, but you are running 2.1.3.

...

  1) Error:
AccountControllerTest#test_activate_api_key:
NoMethodError: undefined method `before_filter' for AccountController:Class
    app/controllers/account_controller.rb:47:in `<class:AccountController>'
    app/controllers/account_controller.rb:46:in `<top (required)>'

  2) Error:
AccountControllerTest#test_add_mugshot:
RuntimeError: @controller is nil: make sure you set it in your test's setup method.
    test/functional_test_case.rb:26:in `post'
    test/controller_extensions.rb:533:in `assert_request'
    test/controller_extensions.rb:202:in `either_requires_either'
    test/controller_extensions.rb:137:in `post_requires_login'
    test/controllers/account_controller_test.rb:418:in `test_add_mugshot'

账户控制器定义:

class AccountController < ApplicationController
  before_filter :login_required, :except => [

应用控制器:

class ApplicationController < ActionController::Base

最佳答案 似乎ApplicationController在AccountController定义期间使用时不是从ActionController :: Base派生的,所以在AccountController声明之前添加require:

# file app/controllers/account_controller.rb
require 'app/controllers/application_controller'

class AccountController < ApplicationController
  before_filter :login_required, :except => [
点赞