Reputation: 4539
I would like to be able to catch messages going to my GenServer's handle_info
in tests, to check those are what I intend to.
1/ Is there a way to print somehow every message coming through?
2/ Using assert_receive
is there a way to catch those messages? Should I set the assert_receive
before or after the call to the external service that will result in the handle_info
trigger? What syntax should I use?
I tried many combinations of assert_receive
and I tried a receive do...
to try and display messages getting in, with no success.
Upvotes: 0
Views: 831
Reputation: 121000
Both ExUnit.Assertions.assert_receive/3
and ExUnit.Assertions.assert_received/2
do assert messages coming into the current process’ mailbox. The former is to be called either before or after the message was actually sent:
Asserts that a message matching
pattern
was or is going to be received within thetimeout
period, specified in milliseconds.
the latter is to be called after:
Asserts that a message matching pattern was received and is in the current process’ mailbox.
That said, both are unlikely a good fit to test the existing GenServer
. Messages are to arrive at the GenServer
’s messagebox, this functionality is provided by OTP and you should not test it. If you need to log messages, add a call to Logger.log/3
to the handle_info/2
and check the log actually happens with ExUnit.CaptureLog.capture_log/2
. If it performs some action upon message arrival, test this action.
In general, you should test your code, not OTP.
Upvotes: 2