belgoros
I have a GenSever that hits an external API every 5 secs. Because of the limited number of API requests, I’m using Docker to run the app locally.
Now even when running tests and even using Mimic library, I need to have Docker API instance running.
I have several questions regarding how to avoid starting/hitting the API locally:
- define a behavior, then 2 different implementations for prod and dev/tests and tweak the config settings to separate the implementation modules depending on the environment
- check somewhere in the code the value of the MIX_ENV to define what to do.
- any other solutions?
Thank you.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
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
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
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
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
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
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
Yep, with e.g.
moxto have contracts and then have dual implementation. I personally dislike that, too much writing and the result somehow does not impress because you also have to have compile-time configuration for test and non-test environments to pick an implementation. To me it’s an expensive abstraction.I prefer
mockorpatchthese days. They are much more transparent and stay out of your way.Mmmm, don’t. No point. If you are going to do that then you might as well use
moxbecause it’s almost the same. Though withmoxyou still keep the two implementations around… but with compilation paths that are different per environment – a standard practice in Elixir, and even newly generated projects have that feature, checkelixirc_pathsinmix.exs).So I day don’t.
Maybe
bypass? I am not impressed by it but many colleagues prefer it, arguing that it’s better to have a half HTTP server than to have none and just mock responses. Huge debate that we best not get into but I thought it’s best to give you another perspective.My personal preference was
mock, also usedpatchonce and liked it. They are interchangeable.Though have in mind the following, taken from
patchREADME:Same applies for
mock.moxallows for async tests.nulltree
Not yet tinkered with the combination but this might fit:
async: falseis the worst. Here’s how to never need it again.LostKobrakai
You cannot work yourself out of the limitations of meck/patch. They’re replacing modules within the beam runtime wholesale to implement their behaviour, which by definition means you cannot use them with concurrent tests.
It’s interesting how dividing this can be. A mock is going to be another implementation and making this a behaviour instead of trying to ignore it will make things more obvious imo. Having a proper interface should also make it clear how each end of the interface is meant to be covered by tests.
There’s also no need to configure things at compile time. Mox doesn’t care how you switch out implementations, only that you do so. E.g. see the mentioned blog post around ProcessTree, where you could pass implementations in with the process start arguments.
dimitarvp
Isn’t that distinction strictly academical?
I mean OK, I find the following worse:
…than this:
But in the end it’s still a visually noisy abstraction. We shouldn’t need to look at our code and say “this is fine”.
You mean the one above my previous comment where
Process.putis utilized? I mean I get it, I’ve used it myself when I really wanted something done and/or it got faster that way (or less visually noisy indeed) but if I can avoid it I’ll absolutely will.BTW I am really curious if you can offer other patterns for having an implementation vary depending on
Mix.env– any others except the two I mentioned? Admittedly I haven’t shopped around for such practices in a long time so might be one of my blind spots.LostKobrakai
I think we got them enumerated here: Compile time selection through app env, runtime selection through app env, process dict (+ hierarchy) or plain passed as value and I guess runtime selection through switching out the module itself instead of the module referenced. Each of them come with their own set of tradeoffs.
Personally I think one should choose the option most suitable to when and how implementations need to be switched out. Though I’m not a fan of meck/patch because they hide the fact that there are multiple implementations from the codebase. Looking at the code you expect Module A to be called, but the code in Module A might never be called by tests. I very much prefer explicit polymorphism of implementations.
Also as with all places where polymorphism is involved you want a properly documented interface, so you can test the code using multiple implementations against the abstract interface as well as test that the actual implementations adhere to the interface. Without that I wouldn’t trust myself debugging a failing test a few month down the line and being able to figure out if the code is wrong or the mock implementation is wrong.
Lastly I tend to find that the things I need to mock are often also things I don’t have access to for dev work as well, so I need to be able to work with multiple implementations anyways.
dimitarvp
You can have and use a
And I agree that when it comes to stuff that’s out of our hands – like 3rd party networked services – then it’s best if we have good contracts, parsing / validation, the whole bag of goodies.
@behavioureven if you don’t usemox.Well OK, it’s hard to disagree on this but how do you cope with the visual noise? Can you give an example with your code? As mentioned above, I find it very non-ergonomic to not just directly call my module with arguments but having to go through a layer of indirection. Would you say you don’t find it as annoying as I do?
LostKobrakai
That. I tend to put selections behind
implfunctions in some places, but more recently I tend to prefer passing those implementations around top to bottom. I guess all OTP behaviours work like that as well. You never callMyGenServer.handle_infoeither and:gen_serverjust passes around a variable for the module being the behaviour implementation.dimitarvp
Hmmm okay, I can see that, though in the case of
GenServerwe’re talking about a “live” entity – a separate process – and in our case we’re talking about a “dead” module that has to invoke the network when its functions are called.But that distinction is likely irrelevant in regards to whether a 3rd party network dependency should accept an implementation as a first argument.
I don’t find the current state of affairs satisfying and I strongly dislike leaky abstractions but I’ll admit that passing an implementation around seems like the least worst way of handling 3rd party network dependencies.
LostKobrakai
Nothing on the beam runs outside a process though. The question is how much work it is to configure the process and if it’s possible to then pass said configuration down the callstack.
I really like that we can be that explicit in elixir especially over abominations like IoC containers, which you might see in OOP languages. I know it takes effort to add the posibility for switching out an implementation for what was previously a static function call, but imo trying to let it stay a “static function call” when it’s no longer that is even worse.
benwilson512
I basically don’t see it. My regular code just calls (as an example)
FlightTracker.get_flights(tracking_number). That module both defines the behavior but also doesSo there is one basically boilerplate module that wraps it all and then the rest of my code doesn’t care. For that, I get async tests, a dev implementation that has to be different than prod anyway due to API limits, and all the caller code is still perfectly clear.