Rails 5 - Could not find generator 'rspec:install'
Asked Answered
M

5

9

In my rails 5.0.0 application I've added the following to my Gemfile:

group :development, :test do
  gem 'byebug', platform: :mri
  gem 'rspec-rails', '~> 3.5', '>= 3.5.2'
end

I ran bundle install. Then gem was successfully installed.

Then I ran following:

rails generate rspec:install

But I got an error saying:

Running via Spring preloader in process 8893
Could not find generator 'rspec:install'. Maybe you meant 'css:assets', 'assets' or 'scaffold'
Run `rails generate --help` for more options.

There are whole bunch of other question posted on this error (Could not find generator 'rspec:install'), but none of them worked for me and it looks like I am having a different issue.

Incase its needed, here's my bundle show output:

Gems included by the bundle:
  * actioncable (5.0.0.1)
  * actionmailer (5.0.0.1)
  * actionpack (5.0.0.1)
  * actionview (5.0.0.1)
  * activejob (5.0.0.1)
  * activemodel (5.0.0.1)
  * activerecord (5.0.0.1)
  * activesupport (5.0.0.1)
  * arel (7.1.2)
  * builder (3.2.2)
  * bundler (1.12.5)
  * byebug (9.0.5)
  * coffee-rails (4.2.1)
  * coffee-script (2.4.1)
  * coffee-script-source (1.10.0)
  * concurrent-ruby (1.0.2)
  * debug_inspector (0.0.2)
  * diff-lcs (1.2.5)
  * erubis (2.7.0)
  * execjs (2.7.0)
  * ffi (1.9.14)
  * globalid (0.3.7)
  * i18n (0.7.0)
  * jbuilder (2.6.0)
  * jquery-rails (4.2.1)
  * listen (3.0.8)
  * loofah (2.0.3)
  * mail (2.6.4)
  * method_source (0.8.2)
  * mime-types (3.1)
  * mime-types-data (3.2016.0521)
  * mini_portile2 (2.1.0)
  * minitest (5.9.0)
  * multi_json (1.12.1)
  * nio4r (1.2.1)
  * nokogiri (1.6.8)
  * pkg-config (1.1.7)
  * puma (3.6.0)
  * rack (2.0.1)
  * rack-test (0.6.3)
  * rails (5.0.0.1)
  * rails-dom-testing (2.0.1)
  * rails-html-sanitizer (1.0.3)
  * railties (5.0.0.1)
  * rake (11.2.2)
  * rb-fsevent (0.9.7)
  * rb-inotify (0.9.7)
  * rspec-core (3.5.3)
  * rspec-expectations (3.5.0)
  * rspec-mocks (3.5.0)
  * rspec-rails (3.5.2)
  * rspec-support (3.5.0)
  * sass (3.4.22)
  * sass-rails (5.0.6)
  * spring (1.7.2)
  * spring-watcher-listen (2.0.0)
  * sprockets (3.7.0)
  * sprockets-rails (3.2.0)
  * sqlite3 (1.3.11)
  * thor (0.19.1)
  * thread_safe (0.3.5)
  * tilt (2.0.5)
  * turbolinks (5.0.1)
  * turbolinks-source (5.0.0)
  * tzinfo (1.2.2)
  * uglifier (3.0.2)
  * web-console (3.3.1)
  * websocket-driver (0.6.4)
  * websocket-extensions (0.1.2)
Moreen answered 17/9, 2016 at 1:54 Comment(0)
M
32

Turns out its an issue with spring gem in my case. Restarting spring solved the problem.

First run bin/spring status. If spring is running you will get something similar:

Spring is running:

    8774 spring server | chessboard | started 14 mins ago   
    8808 spring app    | chessboard 6.52.08 PM | started 12 mins ago | development mode

That means you have to stop spring by running:

bin/spring stop

Then start it again:

bin/spring server

Then run rails generate rspec:install and it should work.

You may need to run bundle update.

Moreen answered 17/9, 2016 at 1:54 Comment(4)
Great! This answer finally pointed me to the fact that Spring server running prevents generators (not just Rspec) to become available to the rails generate command. – Kleon
I had this issue, but just stopping/restarting spring was not enough, needed to run a bundle update after stopping spring too. Then the generator worked. – Ddt
@Ddt Updated the answer based on your comment πŸ™‚ – Moreen
I needed to add -d to run spring in the background. bin/spring server -d – Jotun
P
4

I faced the same problem today and as it turned out I forgot to add rspec gem in my Gemfile.

Adding following line in the Gemfile solved the issue.

gem "rspec-rails"

Needed to do the same for foundation (rails g foundation:install Y)

gem 'foundation-rails'

Once added run bundle to update dependencies.

No need to restart spring.

Pavement answered 15/7, 2017 at 11:30 Comment(2)
Thanks, it was last solution though it worked, yaaay yaaay Mr. Patel!! – Starspangled
Happy to help :) – Pavement
C
2

Just adding a variation to the answer by @dhrubo_moy...

  1. bin/spring stop
  2. bundle update
  3. rails generate rspec:install.

Restarting spring before rspec:install did not work for me.

Clemmie answered 20/1, 2019 at 1:15 Comment(0)
E
1

None of the above answers worked for me.

Replacing rspec gem with below was the only fix I could find.

gem "rspec-core", :github => "rspec/rspec-core"
gem "rspec-expectations", :github => "rspec/rspec-expectations"
gem "rspec-mocks", :github => "rspec/rspec-mocks"
gem "rspec-support", :github => "rspec/rspec-support"
gem "rspec-rails", :github => "rspec/rspec-rails"

I am using ruby 2.5.1 and gem 'rails', '~> 5.0.7', '>= 5.0.7.2'

Empedocles answered 8/5, 2019 at 18:4 Comment(0)
S
0

In my case, Rails 6.1.4.1, Ruby 2.7.4, VSCode IDE, I was adding the gem dependency to Gemfile as:-

gem "rspec-rails"

The problem was eradicated by specifying the version as mentioned in RSpec documentation:-

gem "rspec-rails", '~> 4.0.2'
Saccharate answered 25/8, 2021 at 10:3 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.