【问题标题】:Test failure because of LocalJumpError exception raised (Ruby)由于引发 LocalJumpError 异常而导致测试失败(Ruby)
【发布时间】:2011-08-07 12:32:01
【问题描述】:

我想我搜索了有关我的问题的答案,但我没有看到任何回复它的帖子。所以这里还有一个关于 LocalJumpError 的问题...

我是 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? 测试了该条件。我错过了什么?

感谢您的回答。

【问题讨论】:

    标签: ruby unit-testing exception testunit


    【解决方案1】:

    看起来assert_nothing_raised 需要一个块作为参数。相反,您将调用@logger.get_log('foo') 的结果传递给它。试试这个:

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

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      • 2012-04-10
      相关资源
      最近更新 更多