<div dir="ltr">okay</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jun 6, 2024 at 3:32 PM Tom Chipchase via Chat <<a href="mailto:chat@lists.lrug.org">chat@lists.lrug.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg8326578249034606651"><u></u><div><div>First of all, I wouldn't get too hung up on deciding that because you're doing unit testing, you need to mock B. If its not slow, not difficult to set up in a way that it will return valid responses, and not making requests to external services you don't want to (or can't) do during your CI pipeline, you can just call a real B. (not an exhastive list, there are other valid reasons for mocking).<br></div><div><br></div><div>Secondly, you probably don't want to do `expect(B).to receive(:call).with(x).and_return(y)`. Its conflating two things. If the point of the test is that A does some stuff, and as a result calls B, and you're test is asserting that B is called with some parameters, it doesn't matter what the return object is. If A calls B to get `y` and then do some other things with it, you don't need to `expect` it to happen; something else in your test should fail if it doesn't, so you just need to `allow` instead of `expect`.<br></div><div><br></div><div>Pact has already been mentioned, and that is the path I would go down. I'm not aware of a similar tool that works at the rspec mock level, but you could probably get something close. The key point is that everywhere you have `allow(B).to receive(:call).with(x).and_return(y)`, there should be a corresponding test that asserts that if B is called with x, it does in fact return y. If you combined that with a previous suggestion of defining all your mocks in one place, and using rspec's shared_examples, you could run the same shared example against the real B, and the double of B to ensure they behave the same (or close enough that the tests using your doubles don't care about the difference). The one bit of discipline required would be that if you mock something new, you need to write a spec too.<br></div><div><br></div><div>Lastly, this is a lot easier when the code your testing has been written with this approach in mind from the start. A key thing with pact is that the consumer side is written first, and that's how I'd approach this too. If you're just adding mocks to existing specs in order to isolate them, you'll just end up with very brittle specs.<br></div><div><br></div><div>On Thu, 6 Jun 2024, at 11:51 AM, Patrick Gleeson wrote:<br></div><blockquote type="cite" id="m_8326578249034606651qt"><div dir="ltr"><div>Hi LRUG,<br></div><div><br></div><div>I've been bitten by bad unit tests in the past (mostly ones I've written myself), and lean towards integration tests (a la <a href="https://x.com/rauchg/status/807626710350839808" target="_blank">https://x.com/rauchg/status/807626710350839808</a>) when given the choice. But I want to believe good unit tests are achievable. A problem I often encounter is something like this:<br></div><div><br></div><div>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.<br></div><div><br></div><div>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?<br></div><div><br></div><div>Patrick<br></div><div><i>Mediocre developer. Failed composer. Fledgeling novelist (<a href="https://bedfordsquarepublishers.co.uk/book/hattie-brings-the-house-down/" target="_blank">https://bedfordsquarepublishers.co.uk/book/hattie-brings-the-house-down/</a>).</i><br></div><div><br></div></div><div>_______________________________________________<br></div><div>Chat mailing list<br></div><div><a href="mailto:Chat@lists.lrug.org" target="_blank">Chat@lists.lrug.org</a><br></div><div>Archives: <a href="http://lists.lrug.org/pipermail/chat-lrug.org" target="_blank">http://lists.lrug.org/pipermail/chat-lrug.org</a><br></div><div>Manage your subscription: <a href="http://lists.lrug.org/options.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/options.cgi/chat-lrug.org</a><br></div><div>List info: <a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br></div><div><br></div></blockquote><div><br></div></div>_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org" target="_blank">Chat@lists.lrug.org</a><br>
Archives: <a href="http://lists.lrug.org/pipermail/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/pipermail/chat-lrug.org</a><br>
Manage your subscription: <a href="http://lists.lrug.org/options.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/options.cgi/chat-lrug.org</a><br>
List info: <a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
</div></blockquote></div>