2

在 codeschool 中学习 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。再次重试并返回相同的结果。

有什么线索吗?

预先感谢您的帮助!

4

4 回答 4

3

require_relative 将从与 rspec 文件相同的目录加载文件

于 2014-07-30T20:00:39.900 回答
2

我有几乎同样的问题,虽然我的从来没有工作过一次。更新zombie_spec.rb 文件以显示我的zombie.rb 文件的完整路径似乎让它正常工作。

例如:require "/home/me/ruby/spec/lib/zombie"

于 2013-06-21T03:09:46.660 回答
1

我使用它并且它有效

require_relative "zombie.rb"

于 2014-11-17T14:12:43.497 回答
0

好吧,如果这仍然是一个问题,我遇到了同样的问题,所以我在spec/lib下创建了一个文件夹,我将所有代码和另一个spec/test放在所有测试的位置,然后它就可以工作了。我还添加了 require_relative 这里是代码片段

僵尸规格.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

僵尸.rb

class Zombie
  attr_accessor :name

  def initialize
    @name = "Jack"
  end

  def intelligent?
    false
  end
end
于 2016-01-06T06:55:30.973 回答