Rails 4.0 RuntimeError: @controller is nil: make sure you set it in your test's setup method

4.2k views Asked by At

When I run controller (or functional) tests, Rails 4.0 fails to auto-instantiate a controller instance (@controller) even though the same tests ran fine in Rails 3.2.
Any suggestions about how to begin solving this issue?

Sample output:

 $ 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'

Account Controller definition:

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

Application Controller:

class ApplicationController < ActionController::Base
1

There are 1 answers

1
Малъ Скрылевъ On BEST ANSWER

It seems that ApplicationController isn't derived from ActionController::Base when it was used during definition of the AccountController, so add require before AccountController declaration:

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

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