我正在做一个关于rspec的教程,每当我尝试执行rake测试或通过终端手动运行rspec时都会遇到错误。这是我得到的:
mes-mbp:00_hello Me$ ls
hello.rb hello_spec.rb index.html
mes-mbp:00_hello Me$ rspec 00_hello/hello_spec.rb
/Users/Me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- spec_helper (LoadError)
from /Users/Me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1181:in `block in requires='
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1181:in `each'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1181:in `requires='
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration_options.rb:110:in `block in process_options_into'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration_options.rb:109:in `each'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration_options.rb:109:in `process_options_into'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration_options.rb:22:in `configure'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:96:in `setup'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:85:in `run'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke'
from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/exe/rspec:4:in `<top (required)>'
from /Users/Me/.rvm/gems/ruby-2.2.0/bin/rspec:23:in `load'
from /Users/Me/.rvm/gems/ruby-2.2.0/bin/rspec:23:in `<main>'
from /Users/Me/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `eval'
from /Users/Me/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `<main>'
谁能告诉我我做错了什么?
编辑:有关我正在按要求执行的操作的更多信息: 我正在关注this教程。在上面的代码中,我试图在hello_spec.rb文件中的hello.rb文件上运行测试,该文件中包含“require hello”代码。
答案 0 :(得分:0)
您正在为RSpec提供指定文件的错误路径。
当前工作目录为00_hello
。
mes-mbp:00_hello Me$ rspec 00_hello/hello_spec.rb
将导致RSpec查找00_hello\00_hello\hello_spec.rb
。
如果您运行rspec --init
,则可能有.rspec
个文件,如果该文件包含该行,则该文件需要spec_helper.rb
:
--require spec_helper
编写规范时最常见的做法是将规范放在名为spec
的目录中。这样您就可以通过
$ rspec specs
对于宝石和其他红宝石项目来说,这是一个非常常见的结构:
lib/
hello.rb
spec/
spec_helper.rb
hello_spec.rb
它很常见,RSpec会自动将/lib
和/spec
目录添加到加载路径。
所以你可以这样做:
# spec/hello_spec.rb
require 'spec_helper'
require 'lib/hello' # instead of a relative path! Wehoo.
RSpec.describe Hello do
# ...
end