122 points · by lawik · 2y ago · 39 comments
I will add:
- make sure to “stream” your transfers in general, instead of keeping whole file blobs in RAM. This to avoid refactoring at the least comfortable moment when a file gets too big in production
- it is quite easy to use Mox for isolated testing in general, and it works nicely with ExAws, so make sure to integrate those tests early on
I use it to separate and test external API calls (i.e. to test the payment layer without making any request), to maintain separation between my code and an external library I might want to swap out at a later date, or even between various components of the application. Then writing unit tests becomes a breeze, and you don't need many integration tests.
The extra layer of indirection just for testing bugs me so much. 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.
So much legacy elixir code is an unreadable mess because of all the extra layers of interfaces that only get used for Mox. There’s nothing I dislike more than diving into an unfamiliar section of code and having to keep 15 files open to follow the logic for something that should have been in one module.
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
> 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"
If you have layers of interfaces, it is worth asking why you need mocking across several layers.
YMMV, of course. As long as you are mocking with care, use whatever floats your boat. :)
It's been a minute but I've done it.
The behaviour/implementation setup reminds me of Java, especially when they're exactly the same.
I do recommend only using mox as far down as possible (only around libraries).
Hammox is Mox on steroids, check it out too.
Are you injecting mox or substituting it as a dependency at compile time using an attribute?
Finding the right spot is indeed important here!
defp bucket!, do: System.fetch_env!("BUCKET_NAME")
This will look at that environment variable at build time. For many workflows this doesn't work because you may build the app in CI and run it with different buckets in production and staging.I always default to using Application.get_env/2 and setting the value in runtime.exs, now that that's a thing in recent Elixir.
For things where performance matters in a hot loop, it might be worth compiling in the value, but I wouldn't imagine that's the case here where it's a part of a network call to AWS.
Any calls to System in regular compile time config (e.g. config/prod/dev/test or included) should be forbidden, same for attribute-level / meta code.
I hope we will get this one day!
It is still a good reason to use Application.get_env, and it should be preferred over peppering system environment variable fetches all over the application.
Yeah, runtime here works but I agree is still not as good as Application.get_env. Aside from (IMO) code clarity reasons, last I checked it is (or can be) much quicker. Application is a quick ETS lookup, while System reaches out to the system and I forget the details but at one point that was much slower.
Using mix xref to evaluate your project will, at best, give you a very blurry picture. Think of it more of a database: it will give you good information when you need to answer certain queries.