2011-08-07 70 views
0

我想我搜索了一個關於我的問題的答案,但是我沒有看到任何回覆它的帖子。所以這裏還有一個關於LocalJumpError的問題...由於引發LocalJumpError異常導致測試失敗(Ruby)

我是一個比較新手的Ruby,我剛剛決定遵循好的實踐,並在所有Ruby代碼之前編寫測試代碼。

但我在這裏遇到了一些小問題,我只是不明白。下面是我的測試:

class TestFilesInfoLoggerHashCreation < Test::Unit::TestCase 
    def setup 
     @logger = FilesInfoLogger 
    end 
    # some other tests 
    def test_shall_not_raise_an_exception_if_argument_is_a_string 
    assert_nothing_raise @logger.get_log('foo') 
    end 
end 

這裏是應該確認上述特定的測試代碼:

module FilesInfoLogger 
    extend self 
    def get_log(list) 
    hash = Hash.new {||h,k| h[k] = (block_given?)? yield(k):''} 
    if list.respond_to? :each 
     list.each {|file| hash[file]} 
    else 
     ([]<< list).each {|file| hash[file]} 
    end 
    end 
end 

所以,當我與IRB一切運行FilesInfoLogger.get_log('foo')似乎做工精細,我的意思是什麼被提出。但是,當我運行測試,它無法返回此:

test_shall_not_raise_an_exception_if_argument_is_a_string(TestFilesInfoLoggerHashCreation) [test/files_info_logger_test.rb:43]: 
{"foo"=>""}. 
Exception raised: 
<#<LocalJumpError: no block given (yield)>>. 

爲什麼根據測試單元約沒有給出升高尤其是因爲我測試與block_given?該條件塊例外,我不明白。我錯過了什麼?

感謝您的回答。

回答

0

它看起來像assert_nothing_raised需要一個塊作爲參數。相反,您將調用@logger.get_log('foo')的結果傳遞給它。試試這個:

assert_nothing_raised do 
    @logger.get_log('foo') 
end 
+0

糟糕,我的錯。謝謝。 –

相關問題