Ruby `非#u型选项中的块';:未找到文件:(ArgumentError)
我正在尝试根据传递给脚本的参数打开浏览器url。因此,我编写了以下ruby代码:Ruby `非#u型选项中的块';:未找到文件:(ArgumentError),ruby,unit-testing,testunit,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.g
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
请帮助我理解我做错了什么。似乎测试单元(从1.9.1开始)在其GlobOptions模块中获取了命令行选项。您正在使用ARGV[0]传递浏览器名,但它认为您传递的是文件名。解决方法是捕获ARGV[0]的值,然后在运行测试用例之前清除它:
browser=ARGV[0]
ARGV[0]=nil
您是如何从命令提示符下运行.rb文件的?
ruby test.rb'google'
shell命令的第二个参数被解释为文件而不是ARGV参数。到目前为止,Testunit甚至不涉及您的代码。您正在运行哪个版本的testunit?ruby
是别名吗?如果我理解正确,你的意思是(在我的例子中)$ruby test.rb'google'
test.rb
是ARGV[0],而google
是ARGV[1]。但当我在代码中尝试ARGV[1]而不是ARGV[0]时,我发现了相同的错误。testunit版本,我运行的是2.5.5
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'