How do i get rid of @controller is nil error in my tests

Ruby on-RailsRuby on-Rails-3

Ruby on-Rails Problem Overview


I keep getting

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

When I run my tests. Any idea what this means?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

when you inherit from ActionController::TestCase it infers the controller name from the test name if they do not match you have to use the setup part of test to set it.

So if you have

class PostsControllerTest < ActionController::TestCase
  def test_index
    #assert something
  end
end

Then @controller is auto instantiated to PostsController, however, if this were not the case and you had a different name you would need a setup as such

class SomeTest < ActionController::TestCase
  def setup
    @controller = PostController.new
  end
end

Solution 2 - Ruby on-Rails

I was in the process of upgrading to rspec 3 from the beta version on rails 4 and ran into this error. The problem turned out to be that our Controller spec describe statements used symbols instead of strings. Rspec was attempting to instantiate the symbol as the controller but they were in fact 'actions'.

#trys to set @controller = Index.new
describe SomeController do
  describe :index do
    before do
      get :index, format: :json
    end
    it { expect(response).to be_success}
  end
end

#works
describe SomeController do
  describe 'index' do
    before do
      get :index, format: :json
    end
    it { expect(response).to be_success}
  end
end

Solution 3 - Ruby on-Rails

ErsatzRyan answer is correct, however there is a small typo. Instead of

@controller = PostsController

it should be

@controller = PostsController.new

otherwise you get an error: undefined method `response_body='

Solution 4 - Ruby on-Rails

If the names match, and the @controller variable is still nil, try checking for errors in the controller instantiation. For me I had a controller initialize method that had a bug in it. For some reason the controller was just nil in the test, rather than throwing an error when it wasn't instantiated.

Solution 5 - Ruby on-Rails

Check whether you are ending the do and end properly.

RSpec.describe LeadsController, type: :controller do

   # All tests should come here

end

Solution 6 - Ruby on-Rails

Or, you can simply do this:

RSpec.describe PostsControllerTest, :type => :controller do
  # ...
end

Solution 7 - Ruby on-Rails

Solution 8 - Ruby on-Rails

I encountered this error because I surrounded the controller name in quotes.

# broken
RSpec.describe 'RegistrationsController', type: :controller do
...
end

# works
RSpec.describe RegistrationsController, type: :controller do
...
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmontrealmikeView Question on Stackoverflow
Solution 1 - Ruby on-RailsErsatzRyanView Answer on Stackoverflow
Solution 2 - Ruby on-RailsKevin BView Answer on Stackoverflow
Solution 3 - Ruby on-RailscharlesdebView Answer on Stackoverflow
Solution 4 - Ruby on-RailsNathan HannaView Answer on Stackoverflow
Solution 5 - Ruby on-Railsuser7516728View Answer on Stackoverflow
Solution 6 - Ruby on-RailstokhiView Answer on Stackoverflow
Solution 7 - Ruby on-RailsfguillenView Answer on Stackoverflow
Solution 8 - Ruby on-RailsjohnpitchkoView Answer on Stackoverflow