Rspec-rails: `rails g rspec:install` doesn't add `config.generators.test_framework` setting

Created on 5 Dec 2019  路  6Comments  路  Source: rspec/rspec-rails

What Ruby, Rails and RSpec versions are you using?

Ruby version: 2.7.0preview1
Rails version: 6.0.1
RSpec version: 3.9

Observed behaviour

First...

$ rails new foo
# add rspec-rails to Gemfile
$ bundle
$ rails g rspec:install

then...

$ rails g scaffold posts
      ...
      invoke    test_unit
      create      test/controllers/posts_controller_test.rb
      create      test/system/posts_test.rb
      ...

Expected behaviour

$ rails g scaffold posts
      ...
      invoke    rspec
      create      spec/controllers/messages_controller_spec.rb
      create      spec/views/messages/edit.html.erb_spec.rb
      create      spec/views/messages/index.html.erb_spec.rb
      create      spec/views/messages/new.html.erb_spec.rb
      create      spec/views/messages/show.html.erb_spec.rb
      create      spec/routing/messages_routing_spec.rb
      ...

Obviously this is achieved by adding the following line to the Rails application config:

config.generators.test_framework :rspec

but I am curious鈥攄oes the RSpec team consider this too much hand-holding? In my opinion, if it's not going to be provided automatically by the rspec:install generator, then it should at least be in the README.

Most helpful comment

My goodness, you're right. I think when I tried it yesterday, maybe I hadn't run spring stop, so the generators were running based on the outdated application config? Thanks again for your insight.

All 6 comments

I believe it's here.

Can you reproduce this with rspec-rails version 4.0.0.beta3?

Wow, that was fast!

It appears that in order to take advantage of this, I need to require 'rspec-rails' in /config/application.rb. Is this the correct approach? Is there another way to do it?

As far as I remember, putting this in Gemfile is sufficient:

group :development, :test do
  gem 'rspec-rails', '~> 4.0' # in case of pre-release '4.0.0.beta3'
end

My goodness, you're right. I think when I tried it yesterday, maybe I hadn't run spring stop, so the generators were running based on the outdated application config? Thanks again for your insight.

Okay, one thing I just noticed is that the default generators still create controller specs, even though they are discouraged as of RSpec 3.5.

I ended up adding the following line to my config/application.rb:

generators.test_framework :rspec, controller_specs: false

Is this something that is worthy of fixing? Is a fix already planned for it? I can think of two ways:

  1. disable controller specs from the generator by default; or
  2. modify the rspec-rails.rb file you linked above to use this line instead.

Good point. This is something that聽https://github.com/rspec/rspec-rails/pull/2222 is about to address.

Was this page helpful?
0 / 5 - 0 ratings