Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Rspec `require': cannot load such file -- rails_helper (LoadError)

I am trying to use Rspec for my test. When I run

$ rspec mytest_spec.rb

I get the following error due to the

/home/bastien/.merbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- rails_helper (LoadError)

I have tried many things and somehow if I run

rspec spec

or

bundle exec rspec

from the folder where my .rspec file is I do not get any error. I have created an app just to dry test this issue (I created a new rail app, added rspec in my Gemfile, run the bundle install command and the rspec:install command, generated a scaffold and run the tests. Could anyone explain to me why I get this issue and how I can get rid of it? Do I do something wrong when I try to run only one single spec? Thanks.

like image 265
Bastien Avatar asked Jan 02 '16 16:01

Bastien


3 Answers

For those that the answers above didn't help:

You just need to add gem 'rexml' to your Gemfile.rb, run bundle install and try again.

like image 52
André C. Rocha Avatar answered Nov 20 '22 07:11

André C. Rocha


You are getting that error because you're trying to call your spec like this...

rspec mytest_spec.rb

You need to call it like this from your app's root folder , not inside the spec folder. So first get in the right folder

cd ~/
cd path_to_your_rails_app

Then call your spec

rspec spec/the_rest_of_the_path_to_your_spec/mytest_spec.rb

for instance

rspec spec/models/mytest_spec.rb
like image 18
MilesStanfield Avatar answered Nov 20 '22 07:11

MilesStanfield


For others who find this:

I got this error, 'require': cannot load such file -- rails_helper (LoadError), when I had included the rspec-rails gem but hadn't run rails generate rspec:install which generates the spec/rails_helper.rb file. So if the other solution doesn't help you, make sure you've done that.

like image 6
jim Avatar answered Nov 20 '22 05:11

jim