Ruby Watir 在运行类之外找不到断言方法?
我有一个想要在许多测试用例中使用的类:
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'
缺少什么?谢谢!
I have a class that I want to use in many test cases:
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
However, when run in one of my test cases:
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
I get the error:
NoMethodError: undefined method `assert' for Tests:Class
C:/p4/dev/webToolKit/test/webapps/WhatsIn/ruby-tests/Tests.rb:8:in `Run'
What's missing? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
assert() 是 TestCase 上的实例方法,因此仅适用于测试实例。您在类方法中调用它,因此 Ruby 正在测试中寻找不存在的类方法。
更好的方法是将 Tests 设为模块,将 Run 方法设为实例方法:
然后将 Tests 模块包含在测试类中:
这将使 Run 方法可用于测试,并且 Run() 将找到 assert()测试类中的方法。
assert() is an instance method on TestCase so would only be available to instances of Tests. You are calling it inside a class method so Ruby is looking for a class method in Tests which doesn't exist.
A better way to do this is to make Tests a module and the Run method an instance method:
Then include the Tests module in your test class:
which will make the Run method available to the test and Run() will find the assert() method in the test class.
可能值得尝试将
断言
全部删除,然后仅使用.exists?
。It might be worth a try to remove the
asserts
all together, and just use.exists?
.