Proposal: Add assert_received_exactly to ExUnit

34 views
Skip to first unread message

Tobias Pfeiffer

unread,
Feb 11, 2018, 10:04:22 AM2/11/18
to elixir-l...@googlegroups.com
Hello everyone,

I've happily been using a little helper for quite some time and Devon
liked it as well so thought might propose it:

I often want to make sure that a bunch of messages were received but
exactly these messages and no more.

Enter assert_received_exactly - it gets a list of expected messages and
it expects that each of them arrives no more than specified. (as always
sorry if something like this has been proposed before, haven't seen
anything)

E.g.

assert_received_exactly [:foo, :foo, :bar]

passes if we received :foo 2 times and :bar one time. It fails if we
receive :foo or :bar more often or less often.

Other usage example at [1].

In the current implementation [2] receiving :baz won't fail the test as
only expected values are checked. This is currently done because there
are some messages being received that have nothing to do with the
current thing under test (I stub out some interactions with dummy
modules that send messages instead of doing work like [3]).

I realize it's a very thin wrapper atm but having it part pf ExUnit
would probably still help people and make tests better overall.

Happy to try & convert it to a macro and PR it if this helper is desired
:) (probably also with more custom error messages)

Thanks for considering and for great community work and a great language! :)
Tobi

[1]
https://github.com/PragTob/benchee/blob/3b60c17b067a7e7503cb0fb6e40bdc92ffde3fbd/test/benchee/benchmark/runner_test.exs#L854-L867
[2]
https://github.com/PragTob/benchee/blob/master/test/support/test_helpers.ex#L23-L29
[3] https://devonestes.herokuapp.com/my-new-favorite-elixir-testing-trick
--
http://www.pragtob.info/

José Valim

unread,
Feb 11, 2018, 10:59:25 AM2/11/18
to elixir-l...@googlegroups.com
How is that different from asserting each of them was received followed by a refute_received _? My concern about such helper is that it may imply an ordering which we likely won’t assert. Although one could argue the same about multiple assert_received calls.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/e9f08407-830b-0e38-db5d-34d0672f247c%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
--


José Valim
Founder and 
Director of R&D

Tobias Pfeiffer

unread,
Feb 11, 2018, 12:10:08 PM2/11/18
to elixir-l...@googlegroups.com, José Valim
Right now it's exactly the same as multiple
assert_received/refute_received as this is the implementation atm :D
(https://github.com/PragTob/benchee/blob/master/test/support/test_helpers.ex#L23-L29)

Agreed it somehow implies order which it doesn't (and imo shouldn't)
check. No idea for a better name though - I'd make sure for the docs to
take note of it.

It's just a thin wrapper so yeah that's the question if it's worth to
add it. It's a short cut that I like in its expressiveness but I can see
that this doesn't mean it should be part of ExUnit :)

It could be expanded though Process.info(self(), :messages) to also see
that no other messages were received as a variant or to explicitly allow
other messages to be received. Like: assert that we received exactly
these messages, except for messages that look like this - they are okay
too. Might be too complex for one function as usage though.

Anyhow, just wanted to check if people thought it was a good idea :)
Thanks for the reply!
> <mailto:elixir-lang-core%2Bunsu...@googlegroups.com>.
> *
> *José Valim*
> www.plataformatec.com.br <http://www.plataformatec.com.br/>
> Founder and *
> Director of R&D
> *
> *
>
> --
> You received this message because you are subscribed to the Google
> Groups "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to elixir-lang-co...@googlegroups.com
> <mailto:elixir-lang-co...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JunuWKNwUkuqPD6-fOCLC_WZDLas%2BsmKHE4L978uNzMg%40mail.gmail.com
> <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JunuWKNwUkuqPD6-fOCLC_WZDLas%2BsmKHE4L978uNzMg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

--
http://www.pragtob.info/

Louis Pilfold

unread,
Feb 11, 2018, 12:35:38 PM2/11/18
to elixir-l...@googlegroups.com

You could just get the entire mailbox as a list and then. assert mailbox == [1, 2, 3]
No extra macros required.

Cheers,
Louis


Tobias Pfeiffer

unread,
Feb 11, 2018, 3:54:30 PM2/11/18
to elixir-l...@googlegroups.com, Louis Pilfold
Other messages would interfere there though. Although one could give it
an ignore option and then apparently it'd even keep. I like it for my
purposes (thanks!) - however as often messages are async/valid in many
different orders I'm not sure that's best for everybody.
> <mailto:elixir-lang-core%2Bunsu...@googlegroups.com>.
> *
> *José Valim*
> www.plataformatec.com.br <http://www.plataformatec.com.br/>
> Founder and *
> Director of R&D
> *
> *
>
> --
> You received this message because you are subscribed to the Google
> Groups "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to elixir-lang-co...@googlegroups.com
> <mailto:elixir-lang-co...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JunuWKNwUkuqPD6-fOCLC_WZDLas%2BsmKHE4L978uNzMg%40mail.gmail.com
> <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JunuWKNwUkuqPD6-fOCLC_WZDLas%2BsmKHE4L978uNzMg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google
> Groups "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to elixir-lang-co...@googlegroups.com
> <mailto:elixir-lang-co...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/CABu8xFDhvvijR%3DETv7TFP7cODfgAFGjGjn5iKGpNfTP_aa%3DV-Q%40mail.gmail.com
> <https://groups.google.com/d/msgid/elixir-lang-core/CABu8xFDhvvijR%3DETv7TFP7cODfgAFGjGjn5iKGpNfTP_aa%3DV-Q%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

--
http://www.pragtob.info/

Michał Muskała

unread,
Feb 12, 2018, 1:30:45 AM2/12/18
to Louis Pilfold, elixir-l...@googlegroups.com
One thing that can be used is asserting on the return value of Process.info(self(), :messages) - I consider it a bit "dirty", but it allows easily asserting on the order of messages. I used it couple times in tests, where the order of messages was important.

Michał.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/024a4453-7e1c-3162-8c39-351bc1ab9713%40gmail.com.
Reply all
Reply to author
Forward
0 new messages