seanhandley

seanhandley

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!

Showing Posts 1 to 6

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

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!). :slight_smile:

HansKenis

HansKenis

But passing something in at test time, isn’t that what would make it a Mock/Stub?
And passing it it, could happen with several strategies. Could be a parameter on the function, could be set by config, … It depends on the use case what would be the best strategy, but in both cases Mox is a nice helper, in my opinion. It saves some boilerplate code.

We also create our own behaviour mostly MyApp.Redix and then create a wrapper, MyApp.Redix.Wrapper which implement the functions typically as oneliners.

OvermindDL1

OvermindDL1

Not necessarily, passing things in mean it is pluggable, meaning you can easily change the implementation later if you need to as wlel. :slight_smile:

dch

dch

great approach, maybe you can link to an example of this for people to refer to.

I’d also suggest that you can use mix environments to load the different behaviours in each environment, nicely captured here Mocks and explicit contracts « Plataformatec Blog

seanhandley

seanhandley OP

Thanks for all the great responses folks!

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews