1

按照此处的示例:

http://rspec.info

但是它失败了:

kernel_require.rb:45:in `require': cannot load such file -- bowling.rb (LoadError)

即使我有一个bowling.rb文件。

有什么建议么?

更新

项目清单:

ls -l
-rw-r--r--  1 snowcrash  snowcrash   77 10 Jul 19:43 bowling.rb
-rw-r--r--  1 snowcrash  snowcrash  205 10 Jul 19:49 bowling_spec.rb

$ rspec bowling_spec.rb 
/Users/snowcrash/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- bowling (LoadError)

和代码清单:

规格:

# bowling_spec.rb
require 'bowling'

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

类文件:

# bowling.rb
class Bowling
  def hit(pins)
  end

  def score
    0
  end
end
4

2 回答 2

7

不幸的是,rspec 主页没有告诉您如何在项目中初始化 rspec。

假设您有一个名为“保龄球”的项目文件夹,在保龄球文件夹运行

rspec --init

这将创建规范目录和两个文件

spec/spec_helper.rb
.rspec

.rspec文件可让您定义颜色和格式等首选项

--color
--format documentation

现在spec_helper.rb,添加require "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`.
# Require this file using `require "spec_helper"` to ensure that it is only
# loaded once.

require "bowling"

# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.run_all_when_everything_filtered = true
  config.filter_run :focus

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = 'random'
end

现在在你的里面bowling_spec.rb,添加`require“spec_helper”

require "spec_helper"

class Bowling
  def hit(pins)
  end

  def score
    0
  end
end

此外,您添加的任何其他规范都需要添加require "spec_helper". spec_helper.rb 中的注释解释了为什么这是必要的。

这是设置和使用 rspec的一个很好的初学者解释

祝你好运

于 2013-07-10T21:51:47.583 回答
0

我是一个完整的 ruby​​ 菜鸟(来自 java)并且有一个类似的问题来运行 rspec。上面的答案有助于指出正确的方法,但起初对我不起作用。

阅读提供的链接后,我使用以下解决方案运行它:

第一次运行rspec --init

然后我spec_helper.rbrequire_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

于 2014-09-13T11:36:25.290 回答