Ruby-on-rails 尝试运行 rspec 时出错:`require': 无法加载此类文件 -- rails_helper (LoadError)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25800122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error when trying to run rspec: `require': cannot load such file -- rails_helper (LoadError)
提问by HPJAJ
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 through $ rails g rspec:install
我正在尝试为 Ruby on Rails 运行 rspec。我正在运行 Rails 4.1.1。我已经安装了 gem,已经建立了一个带有一些测试的规范文件夹。我已经创建了一个目录$ rails g rspec:install
I tried to create a testing database through $ rake db:test:preparebut it throws this error message:
我试图通过创建一个测试数据库,$ rake db:test:prepare但它抛出此错误消息:
WARNING: db:test:prepare is deprecated. The Rails test helper now maintains your test
schema automatically, see the release notes for details.
So I ended up looking at this stack overflow post, and of the two options, the one that worked was:
所以我最终查看了这个堆栈溢出帖子,在两个选项中,有效的一个是:
rake db:schema:load RAILS_ENV=test
So, now I need to run rspec.
所以,现在我需要运行 rspec。
When I run $ rspec specfrom the command line I get this error:
当我从命令行运行时$ rspec spec,出现此错误:
/Users/myname/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/
kernel_require.rb:55:in `require': cannot load such file -- rails_helper (LoadError)
How do I resolve this so that I can start running tests?
如何解决此问题以便我可以开始运行测试?
回答by Dipak Gupta
There is some problem with rspec V3. But in your case you are using V2.
rspec V3 存在一些问题。但是在您的情况下,您使用的是 V2。
change
改变
require 'rails_helper'
to
到
require 'spec_helper'
Other description find here https://teamtreehouse.com/forum/problem-with-rspec
其他描述在这里找到https://teamtreehouse.com/forum/problem-with-rspec
For V3 :
对于 V3 :
If someone using rspec V3 then similar error occurs when generator not run. So before trying anything run generator.
如果有人使用 rspec V3,那么在生成器未运行时会发生类似的错误。所以在尝试任何事情之前运行生成器。
rails generate rspec:install
If you are getting a huge list of warning on your console. Then you need to remove --warningsfrom .rspecfile.
如果您在控制台上收到大量警告。然后你需要--warnings从.rspec文件中删除。
回答by fotanus
I actually just had this error on rails 4 with rspec 3, but in my case I forgot to run the generator:
我实际上只是在使用 rspec 3 的 rails 4 上遇到了这个错误,但就我而言,我忘记运行生成器:
rails generate rspec:install
Also I had to remove warningsfrom .rspec, as stated by one of rpsec developers
此外,我不得不删除warnings从.rspec,如由rpsec开发商说
回答by Daniel Loureiro
For me, the problem was due to a different "rspec-support" version loaded and activated. I solved by prepending a "bundle exec":
对我来说,问题是由于加载和激活了不同的“rspec-support”版本。我通过添加一个“bundle exec”来解决:
bundle exec rspec
回答by Hans Araya
For newer versions (3.5), if you need to run the generator, rails generate rspec:installdoesn't work for me. I used:
对于较新的版本 (3.5),如果您需要运行生成器,rails generate rspec:install对我不起作用。我用了:
rspec --init
Creates .rspecand spec/spec_helper.rb.
创建.rspec和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.
编辑:以防万一,我发现我为 ruby 安装了 rspec gem,而不是为 Rails 安装的,所以 rspec:install 不可用。应该使用https://github.com/rspec/rspec-rails。
回答by daveharris
I had the same error but there was no reference to rails_helperanywhere and we weren't even using rails.
我有同样的错误,但没有提到rails_helper任何地方,我们甚至没有使用导轨。
However, I eventually found that my ~/.rspecwas requiring rails_helper(presumably from creating rails app in the past) which was causing the issue ...
但是,我最终发现我的~/.rspec要求rails_helper(大概是过去创建的 rails 应用程序)导致了问题......
$ 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!
修复就像删除该行一样简单。希望这可以帮助其他人解决同样的问题!
回答by Anthony Dreessen
One possibility that worked for me is to make sure you're requiring spec_helper. RubyMine can default require rspecand this can sometimes lead to this error.
对我有用的一种可能性是确保您需要spec_helper. RubyMine 可以默认需要rspec,这有时会导致此错误。
回答by toki
Sometimes you need to run rspec command from Project's parent directory
有时您需要从项目的父目录运行 rspec 命令
回答by mycargus
None of these suggestions worked for me on Rails 4.2.0 with rspec-rails 3.2.3.
这些建议对我在 Rails 4.2.0 和 rspec-rails 3.2.3 上都不起作用。
Instead, I placed the following two lines at the top of my spec file:
相反,我将以下两行放在我的规范文件的顶部:
require 'rails_helper'
require 'spec_helper'
Now I can execute rspec spec/features/my_spec.rb.
现在我可以执行rspec spec/features/my_spec.rb.
回答by thedanotto
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
rake db:test:prepare
回答by Leandro Castro
Always remember to run the
rspecor the bundle exec rspecfrom the root of your project.
Something like:
始终记住从项目的根目录运行
rspec或bundle exec rspec。就像是:
bundle exec rspec spec/features/file_test_spec.rb

