【问题标题】:Why is there an error in the following code?为什么下面的代码有错误?
【发布时间】:2013-10-08 14:42:08
【问题描述】:

如果能帮助我对以下代码中的错误进行排序,我将不胜感激:

require 'rubygems'
require 'watir'
require 'watir-webdriver'
require 'test/unit'

class TestGoogle < Test::Unit::TestCase    
  def setup
    @browser = Watir::Browser.new :firefox
  end

  def testSignInLink
    @browser.goto "http://google.com/"
    po = PageObjects.new(@browser)
    po.clickLinkSignIn
  end
end

class PageObjects
  def initialize( browser )
    @browser = browser
  end

  def clickLinkSignIn()
    @browser.link(:id, "gb_70").click
  end
end

tg = TestGoogle.new
tg.setup
tg.testSignInLink

错误是:

Uncaught exception: wrong number of arguments (0 for 1)
C:/Ruby193/lib/ruby/1.9.1/minitest/unit.rb:971:in `initialize'
C:/RubymineProjects/ditto/Google_01_TU_02.rb:28:in `new'
C:/RubymineProjects/ditto/Google_01_TU_02.rb:28:in `<top (required)>'

第 28 行是:

tg = TestGoogle.new

奇怪的是,脚本随后运行完成,并显示了 google 登录页面。

请注意,目前还没有断言 - 我一次只做一小步。

编辑后添加:

C:/Ruby193/lib/ruby/1.9.1/minitest/unit.rb:971 中的初始化器

  def initialize name # :nodoc:
    @__name__ = name
    @__io__ = nil
    @passed = nil
  end

【问题讨论】:

  • Test::Unit::TestCase 是否要求您在 TestGoogle 类中实现顶级方法? IOW,它是基类中的虚拟吗?
  • TestGoogle 只是从Test::Unit::TestCase 继承initialize。 Test::Unit::TestCase 初始化程序是否需要参数?尽管它看起来可以工作,但测试类中有一些东西不起作用,你可能只是暂时没有使用。
  • @mbratch:这是初始化程序的代码,对我来说几乎没有任何意义:
  • 为什么要手动创建测试用例的实例并运行其方法(即为什么不使用实际的测试/单元运行器)?
  • 请在您的帖子中包含initialize,以便阅读其格式。在我看来,您有一个参数 name 到 initialize,但是,您在没有参数的情况下调用 TestGoogle.new。这将调用不带参数的initialize,因此会出现错误wrong number of arguments (0 for 1)。

标签: watir watir-webdriver testunit


【解决方案1】:

我认为整个问题对所有好的评论者都是不公平的。

原因:作为实验,我把代码的最后3行注释掉了:

tg = TestGoogle.new
tg.setup
tg.testSignInLink

测试运行良好。

我之前假设我需要某种方法来“启动”TestGoogle 类中的方法。类似于依次调用所有方法的“主”程序。

也许这就是@justinko 所指的?那么,TestGoogle 类是一个测试运行器吗?

我想我需要向评论者道歉。

【讨论】:

  • 是的,这就是我所指的。当您require 'test/unit' 时,您的所有测试用例(即 Test::Unit::TestCase 的子级)都会运行。
猜你喜欢
  • 1970-01-01
  • 2016-02-09
  • 2014-10-22
  • 1970-01-01
  • 2015-09-13
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多