我正在使用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()
就行。但是,这要求每个模块都有完全独特的名称,这不是一个好的解决方案。
我缺少什么?