Skip to content

NoMethodError: undefined method `[]=' for nil:NilClass in unit test #839

Closed
@davidjconnolly

Description

@davidjconnolly

Hi, thanks for creating this awesome gem!

I'm trying to unit test my controllers and am getting an error I'm having a really hard time debugging. I've got devise_token_auth configured and am using all the default rails 5 testing gems.

I'm doing what I think is a pretty vanilla test case where I just want to get a list of companies for the currently logged in user. I've verified that the objects are being set up correctly and tried putting loggers/byebug in the controller, however the execution path never makes it that far. Something in my get call is failing but I have no idea what the error it's throwing means: NoMethodError: undefined method []=' for nil:NilClass`

Any suggestions?

test_helper.rb:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  include FactoryGirl::Syntax::Methods
end

class ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers
end

companies_controller_test.rb

require 'test_helper'

class CompaniesControllerTest < ActionDispatch::IntegrationTest

  setup do
    @user = create(:user)
    @company = create(:company)
    @permission = create(:permission, { user: @user, model: @company})
  end

  test "should get company" do
    sign_in @user

    get companies_url
  end

end

rake test output:

I, [2017-02-27T16:50:59.615092 #23039]  INFO -- : ------------------------------------------------
I, [2017-02-27T16:50:59.615143 #23039]  INFO -- : CompaniesControllerTest: test_should_get_company
I, [2017-02-27T16:50:59.615162 #23039]  INFO -- : ------------------------------------------------
I, [2017-02-27T16:50:59.884915 #23039]  INFO -- : Started GET "/companies" for 127.0.0.1 at 2017-02-27 16:50:59 -0700
E

Error:
CompaniesControllerTest#test_should_get_company:
NoMethodError: undefined method `[]=' for nil:NilClass
    test/controllers/companies_controller_test.rb:14:in `block in <class:CompaniesControllerTest>'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions