I think I searched for an answer about my problem but I did not see any post that respond to it. So here is yet another question about LocalJumpError…
I’m a relatively newbie in Ruby and I’ve just decided to follow good pratices and write tests prior to all Ruby stuff I code.
But I got something like a little issue here that I just don’t understand. Here is my test :
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
And here is the code that is supposed to verify the specific test above:
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
So when i run FilesInfoLogger.get_log('foo') with irb everything seems to work fine, I mean nothing is raised. But when i run the test, it fails returning this :
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)>>.
I don’t understand why according to test unit an exception about no block given is raised especially since I test that condition with block_given?. What am I missing?
Thanks for your answers.
It looks like
assert_nothing_raisedexpects a block as an argument. Instead, you are passing it the result of calling@logger.get_log('foo'). Try this: