Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export TestDouble Function in Hex Package #35

Closed
nelsonic opened this issue Apr 2, 2020 · 6 comments
Closed

Export TestDouble Function in Hex Package #35

nelsonic opened this issue Apr 2, 2020 · 6 comments
Assignees
Labels
discuss enhancement New feature or request question Further information is requested technical

Comments

@nelsonic
Copy link
Member

nelsonic commented Apr 2, 2020

At present we are not explicitly exporting the Mock version of HTTPoison in our Hex package.
Which means that the we need to have a Mock implementation in each "consumer" project.
See: https://github.com/dwyl/auth-mvp/blob/9836d31b501a731fcedfdd8a0d4fe026d105bda2/lib/mock/elixir_auth_google.ex#L1-L27

I propose that we take this a step further and actually check the MIX_ENV in the elixir-auth-google package and if MIX_ENV=test we return the Mock version of the functions.
e.g: https://github.com/dwyl/elixir-auth-google/blob/master/lib/httpoison/in_memory.ex
This way the person using the package never has to think about creating their own mocks and simply copy-paste the sample tests to achieve 100% test coverage in their project.

I don't know if this is an "anti pattern" or a "best practice" so I've opened a question to find out:
https://softwareengineering.stackexchange.com/questions/408267/is-it-a-best-practice-or-anti-pattern-to-export-mock-versions-of-functions-for-a
software-engineering-question

Asking questions is the only way to learn. If you aren't asking questions you aren't learning.

I think I want to export the Mocks regardless of what other people say - because I don't want to have to duplicate this code in the Auth project - but I'm curious what the "best practice" is.

@nelsonic nelsonic added enhancement New feature or request question Further information is requested discuss technical labels Apr 2, 2020
@SimonLab
Copy link
Member

SimonLab commented Apr 2, 2020

I propose that we take this a step further and actually check the MIX_ENV in the elixir-auth-google package and if MIX_ENV=test we return the Mock version of the functions.
e.g: https://github.com/dwyl/elixir-auth-google/blob/master/lib/httpoison/in_memory.ex
This way the person using the package never has to think about creating their own mocks and simply copy-paste the sample tests to achieve 100% test coverage in their project.

I like this idea. Having an test example as documentation for the users should be enough for them to cover the authentication testing. At the moment I don't see any cases where the users would want to customise the return values of the mock but it could be added later on if necessary.

@nelsonic
Copy link
Member Author

nelsonic commented Apr 2, 2020

Got a reply on StackExchange:
image

The correct technical term is a "TestDouble".
I'm going to make an attempt at this and will assign the PR when ready. 👍

@nelsonic nelsonic changed the title Export Mock Version in Hex Package Export ~~Mock~~ TestDouble Function in Hex Package Apr 2, 2020
@nelsonic nelsonic changed the title Export ~~Mock~~ TestDouble Function in Hex Package Export TestDouble Function in Hex Package Apr 2, 2020
@nelsonic nelsonic self-assigned this Apr 2, 2020
@nelsonic nelsonic mentioned this issue Apr 2, 2020
2 tasks
nelsonic added a commit that referenced this issue Apr 2, 2020
nelsonic added a commit that referenced this issue Apr 2, 2020
nelsonic added a commit that referenced this issue Apr 2, 2020
@nelsonic
Copy link
Member Author

nelsonic commented Apr 2, 2020

Sadly, Module attribute based dependency injection did not work.
So I had to go searching ...
https://blog.carbonfive.com/2018/03/19/lightweight-dependency-injection-in-elixir-without-the-tears
Using a function to inject the dependency worked. 👍

@nelsonic
Copy link
Member Author

nelsonic commented Apr 2, 2020

PR: #36

@nelsonic
Copy link
Member Author

nelsonic commented Apr 3, 2020

It never ceases to amaze me how some people down-vote questions ...
image
Some people just don't have anything better to do so they have crap on others. 🤦‍♂

@nelsonic
Copy link
Member Author

Meanwhile, there are a couple of answers on my original SoftwareEngineering question.
Exporting a TestDouble is very much a standard practice
and we have applied it to several of our projects now. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss enhancement New feature or request question Further information is requested technical
Projects
None yet
Development

No branches or pull requests

2 participants