4

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
user2069311
  • 134
  • 1
  • 12

1 Answers1

2

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 => [
Felix
  • 4,510
  • 2
  • 31
  • 46
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69
  • 1
    Thank you Малъ Скрылевъ! Adding a require solved the problem. The problem appears to come from files loading in a different order in Rails 4. The application's test_helper file attempted to re-open the definition of ApplicationController. This worked fine in Rails 3. But it looks like Rails 4 loads test_helper before it loads the controllers. So instead of re-opening ApplicationController, the test_helper file gave it an initial definition, which did not derive from ActionController::Base. – user2069311 Dec 04 '14 at 21:46