`block in non_options':找不到文件:(ArgumentError)

时间:2013-09-21 05:57:08

标签: ruby unit-testing testunit

我正在尝试根据传递给脚本的参数打开浏览器URL。因此我写了以下ruby代码:

require 'selenium-webdriver'
require 'test/unit'

class TestTitle < Test::Unit::TestCase
  def setup
    $driver = Selenium::WebDriver.for :firefox
    if ARGV[0] == 'google'
      $driver.get 'http://www.google.com'
    elsif ARGV[0] == 'twitter'
      $driver.get 'http://www.twitter.com'
    end
  end

  def test_title
    puts $driver.title
  end

  def teardown
    $driver.quit
  end
end

当我通过参数:ruby test.rb 'google'时,会导致以下错误:

c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: google (ArgumentError)
        from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
        from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
        from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
        from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
        from c:/Ruby193/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
        from c:/Ruby193/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
        from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:21:in `run'
        from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
        from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
        from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'

请帮助我理解我做错了什么。

1 个答案:

答案 0 :(得分:3)

测试单元(从1.9.1开始)似乎在其GlobOptions模块中抓取命令行选项。您正在使用ARGV [0]传递浏览器名称,但它认为您正在传递文件名。解决方法是捕获ARGV [0]的值,然后在测试用例运行之前清除它:

  

browser = ARGV[0]

     

ARGV[0] = nil