我是一个完整的 ruby 菜鸟(来自 java)并且有一个类似的问题来运行 rspec。上面的答案有助于指出正确的方法,但起初对我不起作用。
阅读提供的链接后,我使用以下解决方案运行它:
第一次运行rspec --init
。
然后我spec_helper.rb
用require_relative '../bowling'
.
看起来像这样:
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause this
# file to always be loaded, without a need to explicitly require it in any files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
require_relative '../bowling'
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
其他两个文件如下所示:
保龄球规格.rb:
# bowling_spec.rb
describe Bowling, "#score" do
it "returns 0 for all gutter game" do
bowling = Bowling.new
20.times { bowling.hit(0) }
bowling.score.should eq(0)
end
end
和保龄球.rb:
# bowling.rb
class Bowling
def hit(pins)
end
def score
-1
end
end
这已经足够了。我发现奇怪的是文档中没有提示rspec --init
首先运行以使其运行。
也许我的安装(rbenv)有问题,所以我必须使用require_relative
?