学习 Ruby,我的 Ruby 应用目录结构遵循 lib/ 和 test/ 的约定
在我的根目录中,我有一个身份验证配置文件,我从 lib/ 中的一个类中读取该文件。它读作 File.open('../myconf')。
使用 Rake 进行测试时,打开的文件不起作用,因为工作目录是根目录,而不是 lib/ 或 test/。
为了解决这个问题,我有两个问题:有可能吗,我应该将 rake 工作目录指定为 test/ 吗?我应该使用不同的文件发现方法吗?虽然我更喜欢约定而不是配置。
库/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
耙文件
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