DRYing Elixir Tests with Macros
hugoribeira.com
hugoribeira.com
None of which means that I don't reserve the right to DRY tests out when I think it's contextually appropriate.
I've been writing Web Applications for a while now. There was only a handful of cases where I needed to repeat the same test case over and over again (authentication being one of them).
If it happens too often, it's a good tell that you're missing a responsability delegation into a concrete module/class somewhere.
On testing, I think DRY principles are a good thing in your production code but I don't believe there is anything wrong with having lots of repetition in your test code. Each test can be read from front to back by the reader. If you have to wander around trying to understand the design of the test then that may not be ideal. Shouldn't need unit testing for your unit tests :P
-- I'm not saying this is the case in the blog post. Just some thoughts.
No perfect answer I guess...
No need to complexify things unless you absolutely must.
This is pretty sweet for testing repetitive tasks and also to get starting with macros on elixir.
But I'll give it a try tonight though :)
Then you just wrap that in your test "..." form.
I don't see much gains from writing a macro, I see more drawbacks.
Pros of just using function and wrapping it in test form:
* It's just a function, easier maintenance. If you write a macro, you have to debug not only the macro but the code your macro writes.
* You gain more clarity into what each test is testing when there are failures. You hard code the "requires authentication" part as your test string. To write a macro that is flexible enough to also allow the user to specify the string here would end up defeating all purpose of it.
* You unquote the three arguments you pass in to the macro immediately. That's a red flag to me that this shouldn't need to be a macro. It's just to avoid the test boilerplate.
Cons:
* It's syntactically longer
I'm coming from a Clojure perspective, I've dabbled briefly with Elixir so let me know if I'm wrong. I wanted to get into Elixir more but the community's pervasive use of macros is keeping me from embracing it. They say Elixir is less magical and more explicit than Ruby and while that's not an untrue statement, I'm seeing too much metaprogramming already with macros. I'm not saying all macros are bad, it's just the vast majority of them are unneeded.
They've made a `make_unauthenticated_request` function that does the work, and the macro just calls it.
But, I agree, I'd probably have skipped the macro. And DRYed it up with just the function.