2 ms·
That just means you're putting a boundary when it's not necessary. Mox exists to make tests simpler to write and reason about, if you keep hitting extra layers
by sph 3y ago
That just means you're putting a boundary when it's not necessary. Mox exists to make tests simpler to write and reason about, if you keep hitting extra layers of indirections then, "you're holding it wrong."
Most things are best used sparingly. That said, the vast majority of developers, even senior, have a very limited understanding of testing practices, and tend to create millions of separated, isolated modules, for no reason whatsoever. That's not Mox's fault.
A stupid example in practice: a naive blog app does not need Mox at all. If you add SSO sign up, you might want to put a boundary there to unit test without needing to set up a fake auth server, but 99% of your code won't ever touch it, just the authentication tests. Tomorrow you add payments, you might want to put a boundary just before your StripeRestAPI module to test the entire payment flow without contacting Stripe in your unit tests. Again, only the payment module tests ever need to touch Mox, the rest of your code doesn't. You place it before modules in the outer edge that have real-world side effects, not to isolate your functional and mostly pure core.
I recommend the DestroyAllSoftware talks, especially https://www.destroyallsoftware.com/talks/boundaries https://www.destroyallsoftware.com/talks/boundaries
> And I just can’t justify it with “one day I might want to swap out the implementation”. YAGNI and if you do, deal with it then.
I agree with that. Using Mox to swap out implementations is a possible use case, but in practice it's better to hardcode things rather than trying to make it generic and swappable the first time around. Junior devs have a tendency to over-engineer code "because we might need to change it later." My motto is "copy and paste it three times before adding a layer of abstraction"
- sarchertech 3y agoI hope you see the irony in using “you’re holding it wrong” here? If real world use of a library tends to push towards a certain outcome that I dislike, I’m going to tend to dislike that library. You’re also never going to convince me that creating an interface for a single implementation is the best way to facilitate unit testing. Mox is nothing more than a kludge to get around the limitations of the language and I think there are much less invasive kludges available. To be honest when I’m working with devs who are new to elixir I’m embarrassed when I have to explain how Mox works. >but in practice it's better to hardcode things rather than trying to make it generic and swappable the first time around. Junior devs have a tendency to over-engineer code "because we might need to change it later." My motto is "copy and paste it three times before adding a layer of abstraction" This I agree with 100%
- gashcrab 3y agoCould you share the less invasive kludges you’ve found? I share your frustration with Mox, although maybe not so passionately.
- sarchertech 3y agoI’ve had a good experience with Mimic for one.
- sph 3y ago> I hope you see the irony in using “you’re holding it wrong” here? Not really. If you use a hammer on a screw, is it the hammer's fault? Sounds like the code base you are working on is badly-organised, rather than Mox being the root of all evil. There are far too many stories of people picking up Elixir and coding in it as if it were Python or Ruby. You cannot base your experience with it on a codebase written by people new to functional languages. And no one forces you to use Mox, anyway. It's far from being a core Elixir component. It is just one implementation of the mock object pattern that comes on a standalone, optional library. > Mox is nothing more than a kludge to get around the limitations of the language Mocks are not an Elixir invention, and exist in pretty much all languages. https://en.wikipedia.org/wiki/Mock_object https://en.wikipedia.org/wiki/Mock_object
- sarchertech 3y ago“You’re holding it wrong” was a response from Steve Jobs blaming users for a design flaw. >Mocks are not an Elixir invention I have no problem with Mocks, I have a problem with Mox. Mox is the kludge, not Mocks in general.
- goosejuice 3y ago> You’re also never going to convince me Such confidence, how do you approach testing your clients? Personally I have never used Mox, but I read the originating blog post when it came out and have followed it's principles ever since.