RAILS -assert_raise 检查异常的深度有多深
以下测试是否应该断言抛出异常?在我的电脑上却没有,我想知道这是否是预期的行为。
def a
raise RuntimeError
end
def b
begin
a
rescue RuntimeError
puts "bummer"
end
end
test "assert this" do
assert_raises RuntimeError do
b
end
end
Should the following test assert an exception was thrown? On my pc it doesn't and I want to know if this is expected behavior.
def a
raise RuntimeError
end
def b
begin
a
rescue RuntimeError
puts "bummer"
end
end
test "assert this" do
assert_raises RuntimeError do
b
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种有意的行为。
assert_raise
不检查脚本执行中的某个位置是否引发异常,它检查块是否引发给定类型的未捕获异常。换句话说,仅当您删除
rescue
语句时它才有效。It's an intended behavior.
assert_raise
doesn't check whether an exception is raised somewhere in the script execution, it checks whether the block raises an uncaught exception of given type.In other words, it works only if you remove the
rescue
statement.