开发者

RAILS - How deep does assert_raise check for an exception

开发者 https://www.devze.com 2022-12-13 19:54 出处:网络
Should the following test assert an exception was thrown? On my pc it doesn\'t and I want to know if this is expected b开发者_如何转开发ehavior.

Should the following test assert an exception was thrown? On my pc it doesn't and I want to know if this is expected b开发者_如何转开发ehavior.

   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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消