2011-01-24 52 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

相关问题