在代码学校运行rspec课程时,我一直遇到同样的问题。我将按照要求进行设置,在创建zombie_spec.rb并运行rspec后,我会得到下面列出的正确输出:
Justins-MacBook-Pro:rubyproject Justin$ rspec spec/lib/zombie_spec.rb
Run options: include {:focus=>true}
All examples were filtered out; ignoring {:focus=>true}
*
Pending:
A Zombie is named Ash
# Not yet implemented
# ./spec/lib/zombie_spec.rb:3
Finished in 0.00929 seconds
1 example, 0 failures, 1 pending
Randomized with seed 7259
当我继续第一个视频并创建类Zombie时,我再次运行rspec时收到此错误:
Justins-MacBook-Pro:rubyproject Justin$ rspec spec/lib/zombie_spec.rb
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- zombie (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/Justin/rubyproject/spec/lib/zombie_spec.rb:2:in `<top (required)>'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
在此之后,我删除了所有内容并卸载并重新安装了rspec。再次重新尝试并返回相同的结果。
有什么想法发生了什么?
提前感谢您的帮助!
答案 0 :(得分:3)
require_relative将从与rspec文件相同的目录中加载文件
答案 1 :(得分:2)
我有同样的问题,虽然我一开始从未工作过一次。更新zombie_spec.rb文件以显示我的zombie.rb文件的完整路径似乎让它正常工作。
例如:require "/home/me/ruby/spec/lib/zombie"
答案 2 :(得分:1)
我使用它并且它起作用了
require_relative“zombie.rb”
答案 3 :(得分:0)
好吧,如果这仍然是一个问题我遇到了同样的问题,所以我在 spec / lib 下创建了一个文件夹,我把所有代码和另一个 spec / test 放在哪里进行所有测试然后它工作。 我还添加了require_relative这里是代码片段
zombie_spec.rb
require 'spec_helper'
require_relative '../lib/zombie'
describe Zombie do
it "has a name called'Jack'" do
zb = Zombie.new
zb.name.should == "Jack"
end
it "has no brains" do
zb = Zombie.new
zb.should be_intelligent == false
end
end
zombie.rb
class Zombie
attr_accessor :name
def initialize
@name = "Jack"
end
def intelligent?
false
end
end