问题描述
按照这里的例子:
但是它失败了:
kernel_require.rb:45:in `require': cannot load such file -- bowling.rb (LoadError)
即使我有一个 bowling.rb
文件.
even though I've got a bowling.rb
file.
有什么建议吗?
更新
项目列表:
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
推荐答案
不幸的是,rspec 主页没有告诉您如何在您的项目中初始化 rspec.
The rspec home page unfortunately does not tell you about initializing rspec in your project.
假设您有一个名为保龄球"的项目文件夹,在保龄球文件夹中运行
Assuming you have a project folder called 'bowling', inside the bowling folder run
rspec --init
这将创建spec目录和两个文件
This will create the spec directory and two files
spec/spec_helper.rb
.rspec
.rspec
文件可让您定义颜色和格式等首选项
The .rspec
file lets you define preferences like color and format
--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"
Now inside your bowling_spec.rb
, add `require "spec_helper"
require "spec_helper"
class Bowling
def hit(pins)
end
def score
0
end
end
此外,您添加的任何其他规范都需要添加require "spec_helper"
.spec_helper.rb 中的注释解释了为什么这是必要的.
Also, any other specs you add you need to add require "spec_helper"
. The comments in spec_helper.rb explain why this is necessary.
祝你好运
这篇关于RSpec: kernel_require.rb:45:in `require': 无法加载这样的文件 -- Bowling.rb (LoadError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!