seanhandley
Testing third party modules with Mox
While writing mocks for an Elixir test suite, I found that mock_with sets up a global mock object which prevents you from safely being able to set a test as async: true.
We’ve been using Mox reliably to mock out our own internal code by setting the @callback attribute on methods we wish to mock, thus treating them as behaviours.
The trouble we’ve hit is when we need to mock a third party module, e.g. Redix (a Redis client). We can patch their library with the relevant @callback attributes, but is there a better and cleaner way to implement unobtrusive, async-friendly mocks for third party code?
It’d be awesome to get a community consensus on this!
Most Liked
benwilson512
You could always add your own Redis behaviour and then have Redix simply be one possible implementation of it. IE, mock MyApp.Redis`` instead of mocking Redix`. That’s what I’ve done for stuff at least.
OvermindDL1
In my opinion, mocking should never ever be done, instead anything that might be mocked should instead be passed into the modules and functions that use it, that way at test time you can pass in something different, this way you retain full async and no surprises about where things come from (plus you might find later that you suddenly need different implementations of something!). ![]()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








