Ruby gem测试中需要错误
我有一个项目树,如下所示:Ruby gem测试中需要错误,ruby,rubygems,gem,Ruby,Rubygems,Gem,我有一个项目树,如下所示: ├── bin ├── fpgrowth-ruby-0.0.1.gem ├── fpgrowth-ruby.gemspec ├── Gemfile ├── Gemfile.lock ├── lib │ ├── fpgrowth │ │ ├── fptree │ │ │ ├── builder │ │ │ │ ├── first_pass.rb │ │ │ │ └── second_pass.rb │ │
├── bin
├── fpgrowth-ruby-0.0.1.gem
├── fpgrowth-ruby.gemspec
├── Gemfile
├── Gemfile.lock
├── lib
│ ├── fpgrowth
│ │ ├── fptree
│ │ │ ├── builder
│ │ │ │ ├── first_pass.rb
│ │ │ │ └── second_pass.rb
│ │ │ ├── fp_tree.rb
│ │ │ └── node.rb
│ │ ├── models
│ │ │ └── transaction.rb
│ │ └── ruby
│ │ └── version.rb
│ └── fpgrowth.rb
├── LICENSE.txt
├── Rakefile
├── README.md
└── test
└── tc_first_pass.rb
在第一次通过的测试用例中,我写道:
require 'test/unit'
require "../lib/fpgrowth/fptree/builder/first_pass"
然后我得到这个:
ruby test/tc_first_pass.rb
/home/damien/.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 -- ../lib/fpgrowth/fptree/builder/first_pass (LoadError)
from /home/damien/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from test/tc_first_pass.rb:2:in `<main>'
ruby测试/tc\u first\u pass.rb
/home/damien/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site\u ruby/1.9.1/rubygems/custom\u require.rb:36:在“require”中:无法加载此类文件--../lib/fpgrowth/fptree/builder/first\u pass(加载错误)
from/home/damien/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site\u ruby/1.9.1/rubygems/custom\u require.rb:36:in'require'
从测试/tc第一次通过。rb:2:in`'
有点不对劲,但我不知道是什么。除非使用
require\u relative
,否则不能要求这样的文件
除此之外,您应该做的是修改
$LOAD\u PATH
以包含./lib使用ruby命令行上的-I标志,在运行时指定所需路径
从您的顶级目录
ruby -I lib test/tc_first_pass.rb
上面告诉ruby解释器仅在该执行的加载路径中包含/lib
那么对于你的需求线
require 'fpgrowth/fptree/builder/first_pass'
对于gem构建和源代码的组织,我建议阅读关于组织源代码、分发和打包代码的章节,这些章节摘自Ruby编程手册,可在此处找到:您可以使用类方法来帮助您
首先是从与cwd
无关的目录开始,而是相对于调用require的文件。它们可能不一样
require File.dirname(__FILE__) + "../lib/fpgrowth/fptree/builder/first_pass"
但是,这不是很容易移植,可以使用join
class方法进行清理:
require File.join(File.dirname(__FILE__), '..', 'lib', 'fpgrowth', 'fptree', 'builder', 'first_pass')
但是你可能会发现你自己把这些都加了进去,不是吗?在这种情况下,考虑在代码< > fpGuest.Rb< /C> >中添加一个帮助器:
def self.root
Pathname.new(File.expand_path(File.dirname(__FILE__)))
end
现在,您可以在任何地方使用该助手:
FpGrowth.root #=> "/absolute/path/to/fpgrowth/lib"
FpGrowth.root.join("fpgrowth", "fbtree", "builder") #=> "/absolute/path/to/fpgrowth/lib/fpbrowth/fbtree/builder"
您是否需要包括文件扩展名,如require./lib/fpgrowth/fptree/builder/first_pass.rb“
?@sawa请不要在提交消息中居高临下。并不是地球上的每个人都在康涅狄格州的小学学习英语。我一定要用$LOAD_PATH技巧来测试我的宝石。还是构建la gem并在我的测试中需要它更好?您可能需要调整$LOAD\u路径进行测试,并在安装gem时让Rubygems处理调整。这使您的测试环境与部署时的环境类似。