[LRUG] Joined-up unit tests

Patrick Gleeson patrick.c.gleeson at gmail.com
Thu Jun 6 03:51:04 PDT 2024


Hi LRUG,

I've been bitten by bad unit tests in the past (mostly ones I've written
myself), and lean towards integration tests (a la
https://x.com/rauchg/status/807626710350839808) when given the choice. But
I want to believe good unit tests are achievable. A problem I often
encounter is something like this:

Service A calls service B. The unit tests for service A mock service B
using statements like "expect(B).to receive(:call).with(x).and_return(y)".
But in reality (due to refactors over time or misunderstandings between
teammates), if you passed x to service B you'd get a return value of z, or
it would raise an ArgumentError. So the unit tests for A are  only passing
because they're describing an impossible situation.

Are there any good patterns for constraining the arguments and return
values stated in mocks somehow? I'd love it if something would check that
the inputs and outputs I specify when mocking service B have been "proven"
to be accurate in my unit tests for B. That way my unit tests could give me
some of the benefits of (slower, unwieldier) integration tests "for free".
Or am I being hopelessly naive and misguided here?

Patrick
*Mediocre developer. Failed composer. Fledgeling novelist
(https://bedfordsquarepublishers.co.uk/book/hattie-brings-the-house-down/
<https://bedfordsquarepublishers.co.uk/book/hattie-brings-the-house-down/>).*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20240606/0f2efe29/attachment.htm>


More information about the Chat mailing list