rails/rails-controller-testing

Gem doesn't supply desired `assigns` or `assert_template` methods, also breaks unrelated tests

mhartl opened this issue · 11 comments

The rails-controller-testing gem appears not to supply the promised assigns and render_template methods, and in addition it appears to break unrelated controller tests. The steps to reproduce are below, and I've also uploaded a rails_controller_testing_app to GitHub for reference.

Here are the initial steps, using the latest beta release of Rails 5:

rails _5.0.0.beta3_ new rails_controller_testing_app
cd rails_controller_testing_app/
rails generate scaffold User name:string email:string
rails db:migrate
rails test

At this point, the tests should be green. Then in test/controllers/users_controller_test.rb we can add uses of assert_template and assigns:

  test "should get index" do
    get users_url
    assert_response :success
    assert_template 'users/index'
    assert_not_nil assigns(:users)
  end

Now rails test gives:

Error:
UsersControllerTest#test_should_get_index:
NoMethodError: assert_template has been extracted to a gem. To continue using it,
        add `gem 'rails-controller-testing'` to your Gemfile.
    test/controllers/users_controller_test.rb:11:in `block in <class:UsersControllerTest>'

After adding gem 'rails-controller-testing' to the Gemfile and running bundle, rails test gives:

Error:
UsersControllerTest#test_should_update_user:
NoMethodError: undefined method `each' for nil:NilClass

Error:
UsersControllerTest#test_should_get_index:
NoMethodError: assigns has been extracted to a gem. To continue using it,
        add `gem 'rails-controller-testing'` to your Gemfile.
    test/controllers/users_controller_test.rb:12:in `block in <class:UsersControllerTest>'

In fact, the first error appears even if there are no occurrences of assigns or assert_template in the test. In other words, the rails-controller-testing gem appears to break unrelated tests in addition to not supplying the desired methods.

I've tried including the gem both inside and outside the :test group in the Gemfile, but it doesn't seem to make a difference either way.

This issue is currently blocking me from upgrading several of my projects to Rails 5, including the Ruby on Rails Tutorial sample app, which otherwise appears to work fine under Rails 5.

Hi @mhartl, can you try with the gem 'rails-controller-testing', :git => 'git://github.com/rails/rails-controller-testing.git'? Should have been fixed by 29b0853 but I'm waiting to confirm one more reported issue before releasing a new version. 😄

Just tested locally. This has been fixed in 29b0853

I can confirm the fix. Thanks! Looking forward to the new release. :-)

I, unfortunately, can't confirm this being fixed.

Gemfile:

source "https://rubygems.org"

gem "rails", github: "rails/rails"
gem "spring"
gem "sqlite3"
gem "sass-rails", "~> 5.0"
gem "uglifier", ">= 1.3.0"
gem "coffee-rails", github: "rails/coffee-rails"
gem "therubyracer", platforms: :ruby
gem "jquery-rails"
gem "turbolinks"
gem "jbuilder", "~> 2.0"
gem "em-hiredis", "~> 0.3.0"
gem "redis", "~> 3.0"
gem "puma"
gem "devise", github: "plataformatec/devise"
gem "haml-rails", "~> 0.9"
gem 'rails-controller-testing', :git => 'git://github.com/rails/rails-controller-testing.git'

group :development, :test do
  gem "pry-byebug"
  gem "rspec-rails", "~> 3.0"
  gem "factory_girl_rails"
end

group :development do
  gem "web-console", github: "rails/web-console"
end
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Spec:

  describe "GET new" do
    it "assigns a new servant_class as @servant_class" do
      get :new, {}, valid_session
      expect(assigns(:servant_class)).to be_a_new(ServantClass)
    end
  end

Result:

  2) ServantClassesController GET new assigns a new servant_class as @servant_class
     Failure/Error: expect(assigns(:servant_class)).to be_a_new(ServantClass)
     NoMethodError:
       assigns has been extracted to a gem. To continue using it,
               add `gem 'rails-controller-testing'` to your Gemfile.
     # /home/quintasan/.rvm/gems/ruby-2.3.0@fgodb/bundler/gems/rails-9201030320d0/actionpack/lib/action_dispatch/testing/test_process.rb:7:in `assigns'
     # ./spec/controllers/servant_classes_controller_spec.rb:58:in `block (3 levels) in <top (required)>'

What am I doing wrong if this actually got fixed?

Hi @Quintasan please provide a sample repro or the exact steps to reproduce the errors you're seeing. Thanks!

@Quintasan I had the same issue. I fixed it by adding the configuration shown here to rails_helper:

https://github.com/rails/rails-controller-testing#rspec

@tgxworld My bad, I actually found instructions mentioned by @lennyk a little after my comment and forgot to report back. Everything works fine now.

I can reproduce this error:

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

whenever I have the gem inside a :test group, but the gem works as expected when no test group is set in the gemfile.

is this the expected behavior?

No. I use this gem every day and I have it inside the test group. Are you able to reproduce this problem in a new application?

Hmm........ the app I got was a sample app from someone else that I made some mods to. It's a Rails 5.1.4 app. Let me try to reproduce on a fresh install Rails 5.1.4 app.

Will push up if I can get it to reproduce on a fresh install Rails app.

@rafaelfranca -- I was able to reproduce this on another attempt;

for this reason, moving to #37