rails/rails-controller-testing

Adding rails-controller-testing** gem does not fix NoMethodError message

jasnow opened this issue ยท 44 comments

Adding rails-controller-testing gem to my Rails 5 fork of codeforatlanta's show-me-the-food5 (https://github.com/jasnow/show-me-the-food5) repo's Gemfile does not fix the following message:

  • NoMethodError:assigns has been extracted to a gem. To continue using it, add gem 'rails-controller-testing' to your Gemfile.

Appears to not be the only one: http://stackoverflow.com/questions/30605868/rails-controller-testing-gem


If you run "rake" before upgrading to the most recent version of Rails 5, no tests break.

After adding the gem rails-controller-testing, running "bundle update", and then running "rake," you get:

Failures:

  1) LocationsController#index assigns @locations
     Failure/Error: subject{ assigns(:locations) }
     NoMethodError:
       assigns has been extracted to a gem. To continue using it,
               add `gem 'rails-controller-testing'` to your Gemfile.
     # /home/jasnow/.rvm/gems/ruby-2.2.2/bundler/gems/rails-7220ffcffd00/
       actionpack/lib/action_dispatch/testing/test_process.rb:8:in `assigns'
     # ./spec/controllers/locations_controller_spec.rb:5:in `block
       (3 levels) in <top (required)>'
     # ./spec/controllers/locations_controller_spec.rb:9:in `block
       (3 levels) in <top (required)>'

Finished in 0.29375 seconds (files took 3.41 seconds to load)
1 example, 1 failure

@JasonW i'll have to fix this. The gem was built under the assumption that the methods would have only been deprecated and not raise a warning. Sorry about that ๐Ÿ˜˜

Question: Will this fix be in this gem or in Rails?

@jasnow I couldn't replicate the issue with the demo app. Could you check if you have the latest version of the Gem installed? Thanks ๐Ÿ˜„

You must run "bundle update" and add the new gem and then run "rake" and see that adding the new gem did not get rid of the message.

Ah ok. This only happens when using RSpec, let me have another look at it.

Hi, I'm using the default Rails testing framework, and I am still seeing the same issue as @jasnow. If you take a look at this repo, you can run rake test on the master branch (rails-4.2.0) to see all the tests pass, and on the rails5 branch (edge rails) to see tests error out. Thank you for your help!

@spark008 Ok I'm seeing the same failures with your app. Could you try out https://github.com/tgxworld/test-app and see if the tests passes for you?

@spark008 Ah I see what is happening for your app. Your app is using a custom test_helper. If you require 'rails-controller-testing', you'll stop getting those errors.

+1, upgraded an existing app to edge rails and tests (Minitest) give the message despite having the gem installed at version 0.0.3.

@redbassett RSpec or Minitest?

Default testing. (So Minitest)

@redbassett could you provide more information? So far, RSpec is known to be broken because RSpec implements assign. I pushed a test app above to demonstrate that it works with edge Rails on Minitest.

Apologies!

I have the following line of code:
assert_equal assigns(:user).remember_token, cookies['remember_token']

It was in place in a test before I upgraded, and passed fine. After the upgrade, I have started getting the following error:

NoMethodError: assigns has been extracted to a gem. To continue using it,
       add `gem 'rails-controller-testing'` to your Gemfile.

This is despite having the gem installed at version 0.0.3. I have not changed the configuration of testing from the default, and this is inside a test block in an integration test.

@redbassett Could you replicate this in a simple app?

@jasnow Ah ok. I found out the reason why RSpec isn't working. RSpec doesn't include ActionController::TestCase but instead includes only ActionController::TestCase::Behavior. What this gem is currently doing is to include the modules under ActionController::TestCase which is why the gem isn't working for RSpec. I'm looking into the fix right now.

Thanks

Hmm I can't figure out a good way to support RSpec out of the box as of now. Rails includes AC::TestCase::Behavior on load so we can't just include modules into AC::TestCase::Behavior. To make it work with RSpec you'll have to include the module manually.

@rafaelfranca Do you have any ideas? Another thing I thought of is to pass the responsibility of including the required modules to https://github.com/rspec/rspec-rails. Thanks in advance! ๐Ÿ˜„

This gem still don't work

@asiniy what doesn't work? I need more information than that to help you.

I use rspec, add

expect(assigns(:current_user)).to eq(user)

And still have this idiotic exception

@tgxworld thanks, I'll try it tomorrow ;)

@tgxworld thanks, works!

In case anyone else runs into this issue, the README file's section about usage with RSpec has since been modified, so the note that @tgxworld is alluding to no longer exists. It can be found, however, in this commit: f756b33

This issue still reproduces for me in apps generated from rails a0a62da2 (i.e. all rails gems at 5.0.0.beta1) and rails-controller-testing v0.0.3. To fix, I put this in my test_helper.rb.

class ActionDispatch::IntegrationTest
  include Rails::Controller::Testing::TestProcess
end

That shouldn't be necessary, right?

Look at @jprince's comment from two days ago: The commit he references was: f756b33

Thanks! (I had ignored that comment because I was looking for a solution for the out-of-the-box Rails tooling, not RSpec. But the work-around is the same.)

U R Welcome

Thanks @andrewjanssen , I had the same issue with Rails 5.0.0.beta1 and this gem (0.0.3), your solution works.

Hmm I think we should add the solution back into the readme for those not running latest RSpec with the fix.

owenr commented

Fixed for paper-trail-gem/paper_trail#689 test_helper.rb (NOT spec_helper.rb) with the following:

if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.0.0.beta1')
  # See https://github.com/rails/rails-controller-testing/issues/5
  ActionController::TestCase.send(:include, Rails::Controller::Testing::TestProcess)
end

Please consider reopening this issue for discoverability.

Using minitest and rails-5.0.0.beta2, I found I had to put the following in test_helper.rb:

class ActionDispatch::IntegrationTest
  include Rails::Controller::Testing::TestProcess
  include Rails::Controller::Testing::TemplateAssertions
  include Rails::Controller::Testing::Integration
end

That's with version 0.1.0 of the rails-controller-testing gem.

@lcreid I fixed the issues with 0.1.0... Aiming for a release soon once I confirm #15 is fixed using master branch

1
56
I get same error,If U can help please? I have been stuck a week.I did add gem and bundle install and update.But It didnt fix it at all.what's wrong?

@jennytech ๐Ÿ‘‹, I see you're using RSpec. The gem requires a bit more config for that setup, make sure you try this from the readme: https://github.com/rails/rails-controller-testing#rspec. Hope that helps! ๐Ÿ˜

@kaspth Do I need add Useage thoes code also?

@jennytech you mean the code under Usage? No, you don't have to add anything but the RSpec config to get that error fixed... hopefully ๐Ÿ˜

I'm on Rails 5.1.4 and rails-controller-testing 1.0.2. I still get the error message "assert_template has been extracted to a gem" from one of my ActionDispatch::IntegrationTest tests. I tried adding to my test_helper.rb:

class ActionDispatch::IntegrationTest
  include Rails::Controller::Testing::TestProcess
  include Rails::Controller::Testing::TemplateAssertions
  include Rails::Controller::Testing::Integration
end

But this results in NameError: uninitialized constant Rails::Controller.

This issue still persists on rails 5.0.0.1, rspec-rails 3.7.2, and rails-controller-testing 1.0.1:

  4) Moderation::MessagesController GET show displays only messages between two users
     Failure/Error: expect(assigns(:conversation_messages).size).to eq(1)

     NoMethodError:
       assigns has been extracted to a gem. To continue using it,
               add `gem 'rails-controller-testing'` to your Gemfile.
     # /Users/maciej/.rvm/gems/ruby-2.2.2/gems/actionpack-5.0.0.1/lib/action_dispatch/testing/test_process.rb:7:in `assigns'
     # ./spec/controllers/moderation/messages_controller_spec.rb:51:in `block (3 levels) in <top (required)>'

Add this to your spec_helper.rb to fix the problem:

require "rails/controller/testing/test_process"
require "rails/controller/testing/integration"
require "rails/controller/testing/template_assertions"

RSpec.configure do |config|
  config.include Rails::Controller::Testing::TestProcess, type: :controller
  config.include Rails::Controller::Testing::Integration, type: :controller
  config.include Rails::Controller::Testing::TemplateAssertions, type: :controller
end

@tgxworld given the above, maybe you could remove the falsey info from the docs? Clearly, adding the gem to the Gemfile alone is not sufficient :(.

This issue still seems to be active with Rails 5.1.6, RSpec 3.7.2, and rails-controller-testing 1.0.2.

I've tried all the solutions here, and nothing has worked. Here's my spec_helper.rb:

...

require 'rails-controller-testing'
require "rails/controller/testing/test_process"
require "rails/controller/testing/integration"
require "rails/controller/testing/template_assertions"

...

RSpec.configure do |config|
  config.include Rails::Controller::Testing::TestProcess, type: :controller
  config.include Rails::Controller::Testing::Integration, type: :controller
  config.include Rails::Controller::Testing::TemplateAssertions, type: :controller
end

...

Still I get the following error:

NoMethodError:
       assigns has been extracted to a gem. To continue using it,
               add `gem 'rails-controller-testing'` to your Gemfile.

Help!

On my project it works fine with controller tests, but when I try to use it within a request spec I get:

NoMethodError:
       assigns has been extracted to a gem. To continue using it,
               add `gem 'rails-controller-testing'` to your Gemfile.

Versions: rails 5.1.4, rspec-rails 3.7.2, rails-controller-testing 1.0.2

Rails 6.0.0.rc1, Ruby 2.6.0p0, still the error persists after adding rails-controller-testing gem

NoMethodError: assigns has been extracted to a gem. To continue using it,
add `gem 'rails-controller-testing'` to your Gemfile.
osfa commented

Rails 6.0.0.rc1, Ruby 2.6.0p0, still the error persists after adding rails-controller-testing gem

NoMethodError: assigns has been extracted to a gem. To continue using it,
add `gem 'rails-controller-testing'` to your Gemfile.

+1

This worked for me:

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'rails-controller-testing'
end