I recently wrote a small example app in Elixir and wanted to add some tests. I quickly realized that I've never tested a
GenServer directly so I had a new thing to learn. 🎉
First naive attempt.
At first I just fired up my
This worked...for the first test. The following tests crashed because it was trying to start a process with the same id.
After some digging I found a post on Elixir Forum where a person had encountered a similar problem. A guy named Jose Valim had answered, stating :
You need to make the name a parameter and pass different ones.
So I messed around with my
Supervisor my tests and my
GenServer with no luck. Even after discovering ExUnit's
start_supervised which sounded like just the thing I needed, I could not spin up a new
GenServer for each test.
So I asked for help on the aforementioned forum post and got some clues from Jose. 👍
Even though the docs for
start_supervised are quite detailed, it wasn't until Jose wrote the following that I connected the dots.
The first argument to
So I ended up with the following
setup in my test:
There might be room for improvement but it does the job of spinning up my
GenServer (worker) for each test.
Now I could write a test like
Thank you Jose, for taking the time to answer questions like mine on the forum. I'm sure you have a lot of stuff on your plate. Being the creator of Elixir and all...
Can be found at https://gitlab.com/smedegaard/heroes_engine