2011-01-24 74 views
0

我有,我想在很多測試情況下使用的一類:Ruby Watir無法在運行類之外找到assert方法?

require 'rubygems' 
require 'test/unit' 
require 'watir' 

class Tests < Test::Unit::TestCase 
    def self.Run(browser) 
    # make sure Summary of Changes exists 
    assert(browser.table(:class, "summary_table_class").exists?) 
    # make sure Snapshot of Change Areas exists 
    assert(browser.image(:xpath, "//div[@id='report_chart_div']/img").exists? ) 
    # make sure Integrated Changes table exists 
    assert(browser.table(:id, 'change_table_html').exists?) 
    end 
end 

然而,在我的測試情況下,一個運行時:

require 'rubygems' 
require 'test/unit' 
require 'watir' 
require 'configuration' 
require 'Tests' 

class TwoSCMCrossBranch < Test::Unit::TestCase 
    def test_two_scm_cross_branch 
    test_site = Constants.whatsInUrl 
    puts " Step 1: go to the test site: " + test_site 
    ie = Watir::IE.start(test_site) 

    Tests.Run(ie) 

    end 
end 

我得到的錯誤:

NoMethodError: undefined method `assert' for Tests:Class 
    C:/p4/dev/webToolKit/test/webapps/WhatsIn/ruby-tests/Tests.rb:8:in `Run' 

缺少什麼?謝謝!

+0

我從來沒有在測試中跑過測試。我可以看到你正在嘗試做什麼。也許你只是把斷言放在父測試中,沒有孩子? – 2011-01-24 22:59:33

回答

2

assert()是TestCase上的一個實例方法,因此只能用於Tests的實例。你在類方法中調用它,所以Ruby在測試中尋找一個不存在的類方法。

一個更好的辦法來做到這一點是要測試一個模塊,Run方法實例方法:

module Tests 
    def Run(browser) 
    ... 
    end 
end 

然後包括在測試類中的測試模塊:

class TwoSCMCrossBranch < Test::Unit::TestCase 
    include Tests 

    def test_two_scm_cross_branch 
    test_site = Constants.whatsInUrl 
    puts " Step 1: go to the test site: " + test_site 
    ie = Watir::IE.start(test_site) 

    Run(ie) 
    end 
end 

這將使Run方法可用於測試,Run()將在測試類中找到assert()方法。

1

這可能是值得嘗試刪除asserts所有在一起,只是使用.exists?

+0

斷言是TestCase的關鍵。沒有它,它會錯誤地說沒有斷言:( – Garrett 2011-01-25 21:54:22