Ruby Watir在运行类之外找不到断言方法?

时间:2011-01-24 20:43:28

标签: ruby unit-testing rubygems watir testcase

我有一个我想在许多测试用例中使用的类:

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'

缺少什么?谢谢!

2 个答案:

答案 0 :(得分:2)

assert()是TestCase上的一个实例方法,因此只能用于测试实例。你在一个类方法中调用它,所以Ruby在Tests中寻找一个不存在的类方法。

更好的方法是将Tests作为模块,将Run方法作为实例方法:

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

然后在测试类中包含Tests模块:

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 :(得分:1)

尝试将asserts全部删除,并使用.exists?可能值得一试。