#Testing for a raised RuntimeError

9 messages · Page 1 of 1 (latest)

worldly linden
#

Hi All, I'm trying to use assert_raise to try to get a crash due to a thrown exception in handle_event in my LiveView. See VSC snapshot attached.
For some reason that I'd like to figure out, this is not working.

#
    test "send a boom here", %{conn: conn} do
      assert_raise RuntimeError, fn ->
        raise "boom"
      end
    end

this above works.

So is it because it's not the current process raising but the genserver running the LiveView?
How can I test that then?

weary thicket
#

Because you want to capture raises and there is exit, not raise coming from that call

#

LiveView lives in separate process, so the raise in the handler will be captured at the process boundary and changed to exit

worldly linden
#

How would you catch the exit then?

worldly linden
#

I tried this and it works but I wonder if there's better

test "send a boom event to the LV", %{conn: conn} do
  {:ok, view, _html} = live(conn, ~p"/users")
  Process.flag(:trap_exit, true)
  spawn_link(fn -> render_click(view, "boom", %{}) end)
  assert_receive {:EXIT, apid, {%RuntimeError{message: "boom"} = err, _stack}}
  dbg({apid, err})
end
weary thicket
#

You do not really need that spawn_link there IIRC

worldly linden
#

If I don't use it the test crashes.