2012-11-12 37 views
0

我剛開始計算如何使用"test/unit"創建單元測試。我複製了Selenium IDE生成的代碼並將其粘貼到我的Ruby測試方法中。驗證在Selenium中不能使用Ruby :: WebDriver

但隨着Ruby.exe運行它時,由於某種原因,它拋出一個錯誤:

Finished tests in 31.835891s, 0.0314 tests/s, 0.0942 assertions/s. 

    1) Error: 
test_method(MyTestClass): 
NameError: uninitialized constant Test::Unit::AssertionFailedError 
    teste-noticia.rb:30:in `rescue in verify' 
    teste-noticia.rb:29:in `verify' 
    teste-noticia.rb:42:in `test_method' 

1 tests, 3 assertions, 0 failures, 1 errors, 0 skips 

任何人都可以幫助我如何正確主張所需的字符串? 歡迎任何良好做法 ;-)。

下面是代碼:

# encoding: utf-8 
require "selenium-webdriver" 
require "test/unit" 

class MyTestClass < Test::Unit::TestCase 

    def setup 
     @driver = Selenium::WebDriver.for :firefox 
     @base_url = "http://www.yoursite.com" 
     @driver.manage.timeouts.implicit_wait = 30 
     @verification_errors = [] 
     @wait = Selenium::WebDriver::Wait.new :timeout => 10 
    end 


    def teardown 
     @driver.quit 
     assert_equal [], @verification_errors 
    end 

    def element_present?(how, what) 
     @driver.find_element(how, what) 
     true 
     rescue Selenium::WebDriver::Error::NoSuchElementError 
     false 
    end 

    def verify(&blk) 
     yield 
     rescue Test::Unit::AssertionFailedError => ex 
     @verification_errors << ex 
    end 

    #your test methods go here 
    def test_method 
     @driver.get(@base_url + "/my-desired-path") 

     verify { assert_equal "Obama wins and will move U.S. forward", @driver.find_element(:css, "h1").text }  

    end 
end 

編輯

我當地的寶石:

C:\Users\wmj>gem list 

*** LOCAL GEMS *** 

addressable (2.3.2) 
bigdecimal (1.1.0) 
childprocess (0.3.6) 
ffi (1.1.5 x86-mingw32) 
io-console (0.3) 
json (1.5.4) 
libwebsocket (0.1.5) 
minitest (2.5.1) 
multi_json (1.3.7) 
rake (0.9.2.2) 
rdoc (3.9.4) 
rubyzip (0.9.9) 
selenium-webdriver (2.26.0) 
test-unit (2.5.2) 

回答

1

我相信這個問題是你所需要的 'MINITEST' 的寶石,但正試圖使用​​'test-unit'gem中的類。 'Minitest'默認安裝在Ruby 1.9中,而不是'Test-Unit'(默認安裝在1.8中)。 Minitest僅部分向後兼容測試單元。

可能的解決方案:

切換到MINITEST:

它是驗證方法,是造成異常的Test::Unit::AssertionFailedError。您可以將其更改爲最小等值,似乎爲MiniTest::Assertion。因此,您的驗證方法將成爲:

def verify(&blk) 
    yield 
    rescue MiniTest::Assertion => ex 
    @verification_errors << ex 
end 

使用測試單位,而不是MINITEST:

假設你已經安裝(gem install test-unit)測試單元寶石,手動指定要使用寶石什麼時候做require 'test/unit'

+0

謝謝。指定'gem「測試單元」'工作! '31.91816秒完成。 1個測試,2個斷言,0個失敗,0個錯誤,0個下降,0個遺漏,0個通知 100%通過 0.03個測試/ s,0.06個斷言/ s' –

相關問題