61

I've broken my rails-rspec. I switched to a different gemset to run a 3rd party test. When I returned to my 2.3.0(default) gemset, I had the following errors.

running rspec gets:

/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require': cannot load such file -- rspec/core/formatters/progress_formatter (LoadError)

running rails generate rspec:install returns:

Could not find generator 'rspec:install'. Maybe you meant 'devise:install' or ...

I have tried uninstalling and reinstalling, but errors persist.

Running rspec -v returns:

- rspec-core 3.6.0
- rspec-expectations 3.6.0
- rspec-mocks 3.6.0
- rspec-rails 3.6.1
- rspec-support 3.6.0

It seems that ruby cannot find rspec-core. I have tried the workaround from this post without success. Thank you in advance for any insight you might provide.

Running

rails 4.2.0, ruby 2.3.0

Rimian
  • 36,864
  • 16
  • 117
  • 117
Sawyer Merchant
  • 1,243
  • 2
  • 12
  • 21

7 Answers7

141

Running bundle exec rspec solved it for me.

lafeber
  • 2,683
  • 1
  • 27
  • 29
  • 15
    If you have multiple versions of `rspec` (maybe different apps with different versions of Rails), `bundle exec` ensures you use the one that's defined in your Gemfile. If you run `rails` or `rake` without that, it's possible your system will try to run a different version. – maxhm10 Jun 22 '18 at 05:42
  • 3
    To expand on above, `bundle exec rspec spec//file_spec.rb`. In other words, run the actual tests with the `bundle exec' prefix. – Martin Sommer Oct 31 '18 at 23:24
  • The issue here isn't running the wrong spec but using the wrong version of rspec – Mark Nov 13 '18 at 16:02
  • 2
    It still doesn't explain why it works. I only have one version of `rspec`. `>gem list --local rspec`: `rspec-core (3.8.0)`. Same version as when I use `rspec -v`. So why would `bundle exec` make any difference? – Chloe Jan 04 '19 at 19:17
  • If you don't want to have to type `bundle exec rspec` every time, do this: Run `gem list | grep rspec` and pay attention to any gems that list multiple versions. then run `gem uninstall ______` for each of those versions (removing ALL of the versions of each). then run `bundle` afterward. This will sync your versions with the versions in your gemfile. – Jeremy Moritz Jan 15 '20 at 13:28
76

Running bundle clean --force did the trick for me, turns out there's some outdated gem in my system

Axel
  • 1,053
  • 9
  • 19
  • 4
    Also worked for me. This should be marked as the correct answer. – Obie Jul 16 '19 at 11:14
  • 1
    This worked for me, but I have no idea what it is doing or why it is working. OP (and future readers) - if you know, that would be very much appreciated. chrs – BenKoshy Sep 16 '19 at 02:15
  • 1
    Looks like running the embedded Rubymine test module broke the command line "rspec ./test.spec" for me. Running it with "bundle exec rspec ./test.spec" also worked, but "bundle clean --force" fixed it. – Mathieu Champagne Aug 12 '21 at 15:44
5

I learned this from Victor Hazbun - expert on CodeMentor and Egghead.io:

Run gem list | grep rspec and pay attention to any gems that list multiple versions.

Then run gem uninstall ______ for each of those versions (removing ALL of the versions of each).

Then run bundle afterward. This will sync your versions with the versions in your gemfile.

Jeremy Moritz
  • 13,864
  • 7
  • 39
  • 43
2

I was getting the error below in a rails app and none of the solutions here worked for me.

gems/rspec-core-3.8.0/lib/rspec/core/formatters.rb:210:in `require': cannot load such file -- rspec/core/formatters/progress_formatter (LoadError)

Emptying the gemset and re-installing did.

This assumes you are using rvm and a .ruby-gemset at the root of your app.

$ rvm gemset empty <name-of-gemset>
$ bundle install
1

I think you should try

bundle exec rspec:install
Prakash Pazhanisamy
  • 997
  • 1
  • 15
  • 25
escray
  • 69
  • 1
  • 5
  • 3
    This is just flat out wrong. Should not be marked as correct answer. – Obie Jul 16 '19 at 11:14
  • As the above commenter pointed out, this solution fails with a command not found error. The syntactically correct command would be `bundle exec rails g rspec:install`, but this still didn't fix this problem for me. – RSmithlal Jul 30 '19 at 22:33
1

I was trying to make a contribution to CocoaPods and was getting the same error when I was doing rspec foo_spec.rb. Turns out they were NOT using rpsec at all. They seem to have had a special test runner:

CocoaPods uses bacon as a test runner. To run all tests, use bundle exec rake spec in the root of the project. If you want to run a specific test instead, use bundle exec bacon spec/[folder]/[name]_spec.rb

For more on that see here

mfaani
  • 33,269
  • 19
  • 164
  • 293
0

If you run the specs from RubyMine, you can specific the RSpec version in Run Configuration.

Configuration Modifications

aristotll
  • 8,694
  • 6
  • 33
  • 53