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

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
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement