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

后端 未结 13 730
青春惊慌失措
青春惊慌失措 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:52

    Please install the following gem:

    gem install 'rspec-rails'

    And then run: rails generate rspec:install

    This will generate the spec folder and rails_helper.rb and spec_helper.rb. Your error should be taken care once this is done.

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

    For newer versions (3.5), if you need to run the generator, rails generate rspec:install doesn't work for me. I used:

    rspec --init
    

    Creates .rspec and spec/spec_helper.rb.

    EDIT: Just in case, I found out that I installed rspec gem for ruby, not the one for Rails, so rspec:install wasn't available. Should use https://github.com/rspec/rspec-rails.

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

    For someone. If you got that error when you trynna test a specific file. you might can like this

    $ bundle exec rspec ./spec/features/user_login_spec.rb
    $ bundle exec rspec ./spec/models/user_login_spec.rb
    
    0 讨论(0)
  • 2020-12-13 04:02

    One possibility that worked for me is to make sure you're requiring spec_helper. RubyMine can default require rspec and this can sometimes lead to this error.

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

    Always remember to run the rspec or the bundle exec rspec from the root of your project. Something like:

    bundle exec rspec spec/features/file_test_spec.rb
    
    0 讨论(0)
  • 2020-12-13 04:08

    I actually just had this error on rails 4 with rspec 3, but in my case I forgot to run the generator:

    rails generate rspec:install
    

    Also I had to remove warnings from .rspec, as stated by one of rpsec developers

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