Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jasnow opened this issue Jun 4, 2015 · 44 comments
Closed
Assignees

Comments

@jasnow
Copy link

jasnow commented Jun 4, 2015

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
@tgxworld
Copy link
Collaborator

tgxworld commented Jun 4, 2015

@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 😘

@tgxworld tgxworld self-assigned this Jun 4, 2015
@jasnow
Copy link
Author

jasnow commented Jun 5, 2015

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

@tgxworld
Copy link
Collaborator

tgxworld commented Jun 6, 2015

@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 😄

@jasnow
Copy link
Author

jasnow commented Jun 8, 2015

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.

@tgxworld
Copy link
Collaborator

tgxworld commented Jun 8, 2015

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

@bunneyster
Copy link

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!

@tgxworld
Copy link
Collaborator

tgxworld commented Jun 9, 2015

@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?

@tgxworld
Copy link
Collaborator

tgxworld commented Jun 9, 2015

@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.

@redbassett
Copy link
Contributor

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

@tgxworld
Copy link
Collaborator

tgxworld commented Jun 9, 2015

@redbassett RSpec or Minitest?

@redbassett
Copy link
Contributor

Default testing. (So Minitest)

@tgxworld
Copy link
Collaborator

@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.

@redbassett
Copy link
Contributor

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.

@tgxworld
Copy link
Collaborator

@redbassett Could you replicate this in a simple app?

@tgxworld
Copy link
Collaborator

@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.

@jasnow
Copy link
Author

jasnow commented Jun 17, 2015

Thanks

@tgxworld
Copy link
Collaborator

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! 😄

@asiniy
Copy link

asiniy commented Aug 21, 2015

This gem still don't work

@tgxworld
Copy link
Collaborator

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

@asiniy
Copy link

asiniy commented Aug 21, 2015

I use rspec, add

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

And still have this idiotic exception

@tgxworld
Copy link
Collaborator

Added a note about usage with RSpec. https://github.com/rails/rails-controller-testing#rspec

@asiniy
Copy link

asiniy commented Aug 23, 2015

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

@asiniy
Copy link

asiniy commented Aug 24, 2015

@tgxworld thanks, works!

@jprince
Copy link

jprince commented Dec 31, 2015

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

@andrewjanssen
Copy link

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?

@jasnow
Copy link
Author

jasnow commented Jan 2, 2016

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

@andrewjanssen
Copy link

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.)

@jasnow
Copy link
Author

jasnow commented Jan 2, 2016

U R Welcome

@metalelf0
Copy link

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

@tgxworld
Copy link
Collaborator

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

@owenr
Copy link

owenr commented Jan 21, 2016

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.

@tgxworld tgxworld reopened this Jan 21, 2016
@lcreid
Copy link

lcreid commented Mar 3, 2016

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.

@tgxworld
Copy link
Collaborator

tgxworld commented Mar 3, 2016

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

@jennytech
Copy link

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?

@kaspth
Copy link
Contributor

kaspth commented May 16, 2016

@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! 😁

@jennytech
Copy link

@kaspth Do I need add Useage thoes code also?

@kaspth
Copy link
Contributor

kaspth commented May 16, 2016

@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 😁

@cheshire137
Copy link

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.

cheshire137 added a commit to cheshire137/competiwatch that referenced this issue Feb 14, 2018
@mdkalish
Copy link

mdkalish commented Apr 16, 2018

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 :(.

@Onikoroshi
Copy link

Onikoroshi commented Jun 8, 2018

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!

@samanthamjohn
Copy link

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

@umesh-bilendo
Copy link

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.

Good725 added a commit to Good725/controller-rails that referenced this issue Feb 19, 2020
@osfa
Copy link

osfa commented May 12, 2020

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

@CornerstoneII
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests