我想我搜索了关于我的问题的答案,但我没有看到任何回应它的帖子。所以这里还有一个关于 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
因此,当我FilesInfoLogger.get_log('foo')
使用 irb 运行时,一切似乎都运行良好,我的意思是没有提出任何问题。但是当我运行测试时,它无法返回:
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?
. 我错过了什么?
感谢您的回答。