【发布时间】:2013-07-18 17:50:19
【问题描述】:
学习 Ruby,我的 Ruby 应用目录结构遵循约定 使用 lib/ 和 test/
在我的根目录中,我有一个身份验证配置文件,我从 lib/ 中的一个类中读取该文件。它读作 File.open('../myconf')。
使用 Rake 进行测试时,打开的文件不起作用,因为工作目录是根目录,而不是 lib/ 或 test/。
为了解决这个问题,我有两个问题: 有可能吗,我应该指定 rake 工作目录来 test/ 吗? 我应该使用不同的文件发现方法吗?虽然我更喜欢约定而不是配置。
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
test/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
耙文件
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 好的,请看我的编辑。
-
谢谢,已添加回复