可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
I tried to create a testing database through $ rake db:test:prepare
but it throws this error message:
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.
When I run $ rspec spec
from the command line I get this error:
/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?
回答1:
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.
回答2:
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
回答3:
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
回答4:
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.
回答5:
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!
回答6:
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.
回答7:
Sometimes you need to run rspec command from Project's parent directory
回答8:
None of these suggestions worked for me on Rails 4.2.0 with 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
.
回答9:
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
回答10:
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...
回答11:
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