学习Ruby,我的Ruby应用程序的目录结构遵循惯例 以lib /和测试/配置位置红宝石和使用耙的单元测试
在我的根目录我有一个认证配置文件,使我从一个读lib /中的类。它被读作File.open('../ myconf')。
使用Rake进行测试时,打开的文件不起作用,因为工作目录是根目录,而不是lib /或test /。
为了解决这个问题,我有两个问题: 是否可能,并且我应该指定rake工作目录来测试/? 我应该使用不同的文件发现方法吗?虽然我更喜欢约定而不是配置。
LIB/A.rb
class A
def openFile
if File.exists?('../auth.conf')
f = File.open('../auth.conf','r')
...
else
at_exit { puts "Missing auth.conf file" }
exit
end
end
测试/ testopenfile.rb
require_relative '../lib/A'
require 'test/unit'
class TestSetup < Test::Unit::TestCase
def test_credentials
a = A.new
a.openFile #error
...
end
end
试图用Rake来调用。我确实设置了一个任务来将auth.conf复制到测试目录,但是结果是工作目录在test /之上。
> rake
cp auth.conf test/
/.../.rvm/rubies/ruby-1.9.3-p448/bin/ruby test/testsetup.rb
Missing auth.conf file
Rake文件
task :default => [:copyauth,:test]
desc "Copy auth.conf to test dir"
task :copyauth do
sh "cp auth.conf test/"
end
desc "Test"
task :test do
ruby "test/testsetup.rb"
end
请添加代码失败+堆栈跟踪 – roody
@roody ok,请参阅我的编辑。 – Rabiees
谢谢,已添加应答 – roody