Guys,
How do we test internal functions? To give you background, we go through
a lot of effort to make sure we split the functions in our modules into
side effect free and side effect full functions. In Erlang, at least,
we test the side effect free functions using things like eunit and quick
check. For the side effect full functions we stand up the system and
run integration tests. We can do that cheaply as we spent a lot of
effort to make sure our systems can be stood up as needed.
The problem is that the side effect full functions tend to be the
external api of the module and declared with 'def'. While the side
effect free functions tend to be internal and declared with `defp`. The
common response is to use mocking and just test the external api, the
internal api will get tested as a side effect. We don't use mocking
unless its mocking a service. I wrote up why at one point and that write
up is easy to find, so I don't want to argue the validity of the
approach here.
So that brings up how we go about it solving this testing problem. I can think of three possible ways to do that.
1) Just use `def` instead of `defp` and document what the external functions are -- Problematic for a number of reasons
2) Write a macro that looks at an environment variable and, if we are in
test exposes everything -- Our test and prod systems would look
significantly different though
3) Split all of our modules into API modules and internal modules --
This is what we do in Haskell and probably the least invasive approach.
I wonder if there are elixir specific pros and cons here I should be aware of, alternatives? etc.
Eric
Sent from
ProtonMail, encrypted email based in Switzerland.