belgoros

belgoros

GenServer: avoid refresh running in tests

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.

Marked As Solved

LostKobrakai

LostKobrakai

By default only files in lib/ are compiled. Test files are scripts and called by the test runner. However in mix.exs you can add additional folders for source files to be compiled. Many elixir projects configure test/support to be such a folder for the test mix env: Add Elixir files to your compiled list - Today I Learned

Also Liked

LostKobrakai

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.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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 does

def get_flights(number) do
  impl().get_flights(number)
end

So 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.

dimitarvp

dimitarvp

Yep, with e.g. mox to 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 mock or patch these 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 mox because it’s almost the same. Though with mox you 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, check elixirc_paths in mix.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 used patch once and liked it. They are interchangeable.

Though have in mind the following, taken from patch README:

Since the global execution environment is altered by Patch, Patch is not compatible with async: true.

Same applies for mock.

mox allows for async tests.

LostKobrakai

LostKobrakai

On a technical level this is indeed different, but on a functional level you have the interface in the form of an http api specification. Instead of switching out a module you’ll be switching out an endpoint. The only downside I see to this is that while you have an interface you’re not in control over the interface. The provider of the http api changing requirements breaks not just the integration with the actual API, but also all the rest of the codebase depending on the interface. If you wrap things in your own abstraction you can limit that blast radius.

This might also be a small push to define the interface by what the application needs rather than what the (current) provider provides.

module PaymentServer.Exchange.MockAlphaVantageApi is not available. This part of the error message is the key. It’s trying to call the correct module, but it’s not available. So the question would be where you put the module definition.

nulltree

nulltree

Not yet tinkered with the combination but this might fit: async: false is the worst. Here’s how to never need it again.

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement