Error when trying to run rspec: `require': cannot load such file — rails_helper (LoadError)

后端 未结 13 729
青春惊慌失措
青春惊慌失措 2020-12-13 03:13

I am trying to run rspec for Ruby on Rails. I am running Rails 4.1.1. I have installed the gem, have established a spec folder with some tests. I have created a directory

相关标签:
13条回答
  • 2020-12-13 03:50

    There is some problem with rspec V3. But in your case you are using V2.

    change

    require 'rails_helper'
    

    to

    require 'spec_helper'
    

    Other description find here https://teamtreehouse.com/forum/problem-with-rspec

    For V3 :

    If someone using rspec V3 then similar error occurs when generator not run. So before trying anything run generator.

    rails generate rspec:install
    

    If you are getting a huge list of warning on your console. Then you need to remove --warnings from .rspec file.

    0 讨论(0)
  • 2020-12-13 03:50

    For me, the problem was due to a different "rspec-support" version loaded and activated. I solved by prepending a "bundle exec":

    bundle exec rspec
    
    0 讨论(0)
  • 2020-12-13 03:50

    You may also have your file named incorrectly. Make sure it ends in _spec.rb. I had misspelled my file to end in _soec.rb and was scratching my head for a while...

    0 讨论(0)
  • 2020-12-13 03:51

    I had the same error but there was no reference to rails_helper anywhere and we weren't even using rails.

    However, I eventually found that my ~/.rspec was requiring rails_helper (presumably from creating rails app in the past) which was causing the issue ...

    $ cat ~/.rspec
    --color
    --format documentation
    --require spec_helper
    --require rails_helper
    

    The fix is as simple as removing that line. Hope this helps someone else grappling with the same issue!

    0 讨论(0)
  • 2020-12-13 03:51

    Sometimes you need to run rspec command from Project's parent directory

    0 讨论(0)
  • 2020-12-13 03:51

    I got the error you described when running a controller test. However, when I ran a model test, I got the true error. it ended up saying that the error was occuring because my database wasn't migrated. I fixed the error by running this command...

    rake db:test:prepare

    0 讨论(0)
提交回复
热议问题