使用Ruby-1.9.3 ...如何在Ruby中將塊傳遞給assert_raise?
我讀過一些規範的博客文章對塊&特效的主題,但我不明白爲什麼這兩種情況是不同的:
module TestHelpers
def bad_arg_helper(&scenario)
# this works fine, test passes (without the next line)
assert_raise ArgumentError, "wrong type of arguments, pass Fixnum or Range parameters", { Myclass.myfunc "bad" }
# even though the exception is correctly raised in myfunc, this assert does not see it and the test fails
assert_raise ArgumentError, "wrong type of arguments, pass Fixnum or Range parameters", scenario.call
end
end
class TestClass < Test::Unit::TestCase
include TestHelpers
def test_bad_args
bad_arg_helper { Myclass.myfunc "bad" }
end
end
如何將一個塊傳遞給另一個模塊中的測試助手?
嘗試'&scenario',而不是在第二行'scenario.call'。 –
這樣做......我還沒有看到一個例子,其中'&'在參數列表之外使用。爲什麼'method.call'不能在這裏工作?什麼時候在方法體中使用'&'是合適的?請隨時爲信用回答,並在任何情況下,謝謝。 – jordanpg
查看knut的回答:) –