"@controller is nil" error in ActionController::TestCase #5

Open
sunaku opened this Issue Jan 13, 2011 · 4 comments

Comments

Projects
None yet
2 participants
@sunaku
Contributor

sunaku commented Jan 13, 2011

When using context() inside an ActionController::TestCase, I get the following error:

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

Here is an example of what my test file looks like:

class ExamplesControllerTest < ActionController::TestCase
  context "index action" do
    should "return a list of examples" do
      get :index, :format => :json
      assert_response :success
    end
  end
end

The error is happening because context() creates a new Test::Unit::TestCase derived class whose name does not contain "ExamplesController", and so Rails becomes confused when it tries to automatically set @controller inside of the context() body.

Thanks for your consideration.

@ghost

This comment has been minimized.

Show comment Hide comment
@ghost

ghost Jan 14, 2011

A workaround / solution is to assign @controller an instance of the controller you're testing in a block passed to setup().

ghost commented Jan 14, 2011

A workaround / solution is to assign @controller an instance of the controller you're testing in a block passed to setup().

@retr0h

This comment has been minimized.

Show comment Hide comment
@retr0h

retr0h Mar 30, 2011

Is this gem so awesome, that no one maintains it anymore?

retr0h commented Mar 30, 2011

Is this gem so awesome, that no one maintains it anymore?

@sunaku

This comment has been minimized.

Show comment Hide comment
@sunaku

sunaku Mar 30, 2011

Contributor

It is indeed disheartening, @retr0h. There is always opportunity for improvement / increasing popularity!

Contributor

sunaku commented Mar 30, 2011

It is indeed disheartening, @retr0h. There is always opportunity for improvement / increasing popularity!

@sunaku

This comment has been minimized.

Show comment Hide comment
@sunaku

sunaku Jun 3, 2011

Contributor

I wrote the following monkeypatch to automate @robgleeson's suggestion of setting @controller as a workaround for the problem:

class ActionController::TestCase
  def self.context *args, &block
    super *args do
      setup do
        # establish the rails controller being tested
        # https://github.com/citrusbyte/contest/issues/5#issuecomment-677003
        ancestors = self.class.ancestors
        test_case = ancestors[ancestors.index(ActionController::TestCase) - 1]
        @controller = test_case.controller_class.new
      end
      class_eval &block
    end
  end
end

Put that in your test/test_helper.rb and things will Just Work. Cheers.

Contributor

sunaku commented Jun 3, 2011

I wrote the following monkeypatch to automate @robgleeson's suggestion of setting @controller as a workaround for the problem:

class ActionController::TestCase
  def self.context *args, &block
    super *args do
      setup do
        # establish the rails controller being tested
        # https://github.com/citrusbyte/contest/issues/5#issuecomment-677003
        ancestors = self.class.ancestors
        test_case = ancestors[ancestors.index(ActionController::TestCase) - 1]
        @controller = test_case.controller_class.new
      end
      class_eval &block
    end
  end
end

Put that in your test/test_helper.rb and things will Just Work. Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment