我正在尝试为CodeEval上的挑战练习编写测试。每个挑战我正在创建一个文件夹,在项目文件夹中创建'lib'& 'spec'文件夹。从命令行我运行:
rspec --init
它告诉我'spec / spec_helper.rb'& '.pecpec'已创建。我把源文件放在'lib'文件夹& 'spec / lib'文件夹中的spec文件&相应地命名他们。这是一个规范的例子:
require 'spec_helper'
require 'file_name'
describe '#swap_elements' do
it 'should swap 1 pair of numbers' do
swap_elements('1 2 3 4 5 6 7 8 9',0,8).should == '9 2 3 4 5 6 7 8 1'
end
end
从项目的根目录运行:
rspec /spec/lib/file_name_spec.rb
,结果如下:
/Users/username/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load': cannot load such file -- /spec/lib/swap_elements_spec.rb (LoadError)
from /Users/username/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /Users/username/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /Users/username/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /Users/username/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /Users/username/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /Users/username/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
这是最近的问题&我还没有安装任何更新的rspec宝石。知道为什么会这样吗?
答案 0 :(得分:5)
在路径的开头使用斜杠表示根。
你想做
rspec spec/lib/file_name_spec.rb
因此它将搜索当前目录