在'require'中:没有要加载的文件 - spec_helper

MBD*_*BDJ 32 windows rspec ruby-on-rails

使用jruby 1.6在Windows平台上使用rspec在rails上导航教程部分尝试使用ruby时遇到此错误:

c:\rails_projects\sample_app>bundle exec rspec spec/
org/jruby/RubyKernel.java:1038:in `require': no such file to load -- spec_helper
 (LoadError)
        from c:/rails_projects/sample_app/spec/controllers/pages_controller_spec    .rb:1:in `(root)'
        from org/jruby/RubyKernel.java:1063:in `load'
        from c:/rails_projects/sample_app/spec/controllers/pages_controller_spec
.rb:386:in `load_spec_files'
        from org/jruby/RubyArray.java:2458:in `collect'
        from c:/jruby-1.6.0.RC2/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspe
c/core/configuration.rb:386:in `load_spec_files'
        from c:/jruby-1.6.0.RC2/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspe
c/core/command_line.rb:18:in `run'
        from c:/jruby-1.6.0.RC2/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspe
c/core/runner.rb:55:in `run_in_process'
        from c:/jruby-1.6.0.RC2/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspe
c/core/runner.rb:46:in `run'
        from c:/jruby-1.6.0.RC2/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspe
c/core/runner.rb:10:in `autorun'
        from org/jruby/RubyProc.java:268:in `call'
        from org/jruby/RubyProc.java:232:in `call'
Run Code Online (Sandbox Code Playgroud)

找到教程中缺少的答案:

c:\ rails_projects\sample_app> rails生成rspec:install

Gre*_*egC 55

跑完之后

rails generate rspec:install
Run Code Online (Sandbox Code Playgroud)

*_spec.rb文件放在(在您的示例中)c:\rails_projects\sample_app\spec\model.然后使用require_relative指定相对路径

require_relative '../spec_helper'
Run Code Online (Sandbox Code Playgroud)

  • 不要使用`require_relative` - 这意味着当你移动测试时,你必须不断更新路径.只需使用`require'spec_helper'`即可. (5认同)

MBD*_*BDJ 12

运行以下命令

c:\rails_projects\sample_app>rails generate rspec:install
Run Code Online (Sandbox Code Playgroud)

这会将spec_helper.rb文件放在您的/spec目录中