我正在使用Watir的Ruby程序,它有一系列模塊。無法通過作用域操作員訪問模塊中的函數
一個例子模塊看起來像:
demoMod1.rb
DEMOMOD1 = true #this module has been loaded
module demoMod1
def reload(browser)
browser.goto("demo1.html")
end
end
有幾個文件非常類似命名的功能(例如:reload()
),所以demoMod2和demoMod3都具有相同的裝載功能(除了指向不同的頁面)。
當預計不會測試該區域時,模塊將從需求列表中註釋掉。 (根據設計規格)
程序然後動態加載測試用例文件(代碼在main.rb中執行此操作)。每個測試用例文件都包含一個runTest()
函數,該函數調用模塊中的相應部分。每個測試都會檢查模塊是否已加載,如果有,則會執行測試。
樣本測試案例文件看起來像:
test1.rb
def runTest(browser) # test case 1
if(defined?(DEMOMOD1)).nil?
return nil #if the module is not there, dont run this test
end
demoMod1::reload(browser)
end
按照規格,單個文件列出了所有的模塊進行必需的,以便用戶可以直接發表評論沒有必要查看主文件,將這些部分放在不被測試的位置。主要文件如下。
main.rb的
require 'Watir'
SITE = #internal site - address removed =/ sorry
FOLDER = File.dirname(__FILE__) + '/lib'
SHORTNAME = 'Test' #for reporting
LONGNAME = 'In Development' #for reporting
#I know - globals.. yuck... avoid...
require FOLDER + '/lib/requires'
#includes the function to require each module from the module folder
require FOLDER + '/www/include/report.module'
#creates an html report to view the test results
# Get list of tests
def getTests()
Dir.chdir(FOLDER + '/Tests')
files = Dir.glob("*.rb")
return files
end #eofunc
# Run each test
def runTests(files, browser, report)
testSuite = String.new
files.each do |test|
suite = test.split(" - ") # name convention: suite - testname.rb
if !(suite[0]==testSuite)
testSuite = suite[0]
report.newTestSuiteName(testSuite)
end #eoif
load FOLDER + '/Tests/' + test #load the test case file
runTest(browser) #run the test case
end #eoeach
end #eofunc
begin
browser = Watir::Browser.new
browser.goto(SITE)
report = Report.new()
reportname = report.createReport(SHORTNAME, LONGNAME) #vars in Vars.rb
files = getTests()
runTests(files, browser, report)
report.finishReport(reportname, SITE)
browser.close
report.openReport(reportname) #launch report in a new browser
end #eofunc
我會在每個runTest()
的期望能夠訪問demoMod1::reload()
和demoMod2::reload()
,但這種情況並非如此。我收到undefined method 'reload' for demoMod1:Module
的錯誤。我可以通過使用include demoMod1
來解決錯誤,但是(如果我有這個正確的話),範圍是不需要的,只需要使用reload()
就行。但是,這要求每個模塊都有完全獨特的名稱,這不是一個好的解決方案。
我缺少什麼?