Bas*_*ien 5 ruby rspec ruby-on-rails
我正在尝试使用Rspec进行测试.我跑的时候
$ rspec mytest_spec.rb
Run Code Online (Sandbox Code Playgroud)
由于这个原因,我得到以下错误
/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)
Run Code Online (Sandbox Code Playgroud)
如果我跑的话,我已经尝试了很多东西
rspec spec
Run Code Online (Sandbox Code Playgroud)
要么
bundle exec rspec
Run Code Online (Sandbox Code Playgroud)
从我的.rspec文件所在的文件夹中我没有收到任何错误.我创建了一个应用程序只是为了干这个问题测试(我创建了一个新的铁路应用程序,在我的Gemfile中添加了rspec,运行bundle install命令和rspec:install命令,生成了一个脚手架并运行测试.有人可以向我解释为什么我会遇到这个问题以及如何摆脱它?当我尝试只运行一个规格时,我做错了吗?谢谢.
Mil*_*eld 12
您收到该错误是因为您试图像这样调用您的规范...
rspec mytest_spec.rb
Run Code Online (Sandbox Code Playgroud)
您需要从应用程序的根文件夹中调用它,而不是在spec文件夹中.所以先进入正确的文件夹
cd ~/
cd path_to_your_rails_app
Run Code Online (Sandbox Code Playgroud)
然后拨打您的规格
rspec spec/the_rest_of_the_path_to_your_spec/mytest_spec.rb
Run Code Online (Sandbox Code Playgroud)
例如
rspec spec/models/mytest_spec.rb
Run Code Online (Sandbox Code Playgroud)
对于发现此问题的其他人:
'require': cannot load such file -- rails_helper (LoadError)当我包含 rspec-rails gem 但没有运行rails generate rspec:install生成spec/rails_helper.rb文件时,我收到了这个错误。因此,如果其他解决方案对您没有帮助,请确保您已经这样做了。