RAILS -assert_raise 检查异常的深度有多深

发布于 2024-08-14 15:45:41 字数 290 浏览 3 评论 0原文

以下测试是否应该断言抛出异常?在我的电脑上却没有,我想知道这是否是预期的行为。

   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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘和柠 2024-08-21 15:45:41

这是一种有意的行为。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文