gpt4 book ai didi

ruby - RSpec: kernel_require.rb:45:in `require' : 无法加载此类文件 -- bowling.rb (LoadError)

转载 作者:太空宇宙 更新时间:2023-11-03 17:16:40 28 4
gpt4 key购买 nike

按照这里的例子:

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)

和代码 list :

规范:

# 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 的信息。

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

rspec --init

这将创建 spec 目录和两个文件

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 中的注释解释了为什么这是必要的。

这是一个很好的初学者解释 setting up and working with rspec

祝你好运

关于ruby - RSpec: kernel_require.rb:45:in `require' : 无法加载此类文件 -- bowling.rb (LoadError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17578575/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com