2012-01-04 44 views
0

我正在使用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()就行。但是,這要求每個模塊都有完全獨特的名稱,這不是一個好的解決方案。

我缺少什麼?

回答

0

通過更改模塊中的函數名稱,我可以得到它的工作。

例如模塊:

DEMOMOD1 = true 
module DemoMod1 
    def DemoMod1.reload(browser) #<--------- 
    browser.goto('demo1.htm') 
    end 
end 

符合< ---箭頭是主要的變化(也不得不改變模塊的名字,沒有一個大寫首字母...哎呀)

示例測試用例:

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) 
    ### OR ### Both work 
    DemoMod1.reload(browser) 
end 
0

您是否嘗試過使用Test/Unit/rSpec或類似的軟件來運行測試?

除此之外,我的理解是:

需要文件模塊處於僅使其可列入。你需要使用include將它放到一個類中才能使用它。

您是否考慮過爲您的測試製作課程,例如: Test,然後TestType1TestType2繼承自該父類?

例如:

require fileWithYourModules 

class Test 
# stuff I want to have available to all tests 
end 

class TestSubType1 < Test 
include demoMod1 
end 

class TestSubType2 < Test 
include demoMod2 
end 

,然後讓你調用相同的方法名稱和它有不同的表現取決於測試類有。

我確定有更優雅的解決方案,我只是一個測試人員而不是程序員!:)

相關問題