itssasanka

itssasanka

Load all files from a directory and get the modules in those files

Hi,

I am using mocking in my unit tests in my code. My mocks are however, injected through Application config,
For example, if Application.get_env(:app, :httpclient) is HTTPoison in my development and production, it would be a special dummy module called HTTPoisionMock I created for testing purposes. This module has exactly the same API as HTTPoison but it’s function calls can be mocked.

Now, at the beginning of my test run, I need to initialize these mocks, meaning, I need to call a init() function on each of them so that they are “registered”.

I would like to see if there is a way for the for the test_helper.exs to automatically get all the modules from test/mocks/ directory and call .init() on each of them?

currently I am manually maintaining a list of these modules and doing init in the following way:

mocks = 
 [ 
  Mocks.MailerMock,
  Mocks.AccountsMock,
  Mocks.HTTPoisonMock,
]
Enum.each mocks, fn(m)->
  m.init()
end

I would like to get rid of all this manual maintenance and let the code do it.
Any help would be appreciated, thank you!

Most Liked

hauleth

hauleth

If all modules you are using have common prefix then you can use :code.all_loaded() and filter modules that match given prefix. Alternatively you can just list all mocked variables in Application.config and use that, either via Application.get_env or via Application.get_all_env.

However I would avoid global mocks if you can, especially when these mocks have global state (I assume that they have as you need globally call init/0 on them).

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
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
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement