學習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