要求无法加载此类文件

时间:2014-04-21 01:30:22

标签: ruby tdd

我在同一个目录中有以下两个文件:

churn.rb:

 subsystem_names = ['audit', 'fulfillment', 'persistence',    
                     'ui', 'util', 'inventory']
  start_date = month_before(Time.now)       

  puts header(start_date)                   
  subsystem_names.each do | name |
    puts subsystem_line(name, change_count_for(name))   
  end

churn_test.rb:

require "test/unit"
require "churn"    

class ChurnTests < Test::Unit::TestCase 

  def test_month_before_is_28_days
    assert_equal(Time.local(2005, 1, 1),
                 month_before(Time.local(2005, 1, 29)))
  end

end

当我运行churn_test.rb时,我收到以下错误:

/Users/ca/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- churn (LoadError)
    from /Users/ca/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from churn-tests.rb:8:in `<main>'

1 个答案:

答案 0 :(得分:1)

当您require sth时,Ruby会在$LOAD_PATH$:)中搜索来源。出于安全考虑,当前工作目录不包含在$LOAD_PATH中。您可以将目录明确添加到$LOAD_PATH

$: << File.dirname(__FILE__)

require 'churn'

或使用Kernel#require_relative来要求基于同一目录的模块:

require_relative "churn"