Since ExUnit doesn’t support nested describe/context blocks, what are some alternative strategies to describe these more complex sets of contexts?
Your first approach is the alternative. Sometimes we get too hung up on reducing duplication and we forget the downsides of trying to “unify” everything, such as coupling and loss of context. ExUnit wants you to define everything in two lines (the describe and setup) because the next person reading the file will have a better understanding of what is happening.
Go back to a project that uses nested describes/context and try to find out what a single test is doing. This is going to be the process:
- You will find the test and try to find the outer most describe
- Put “user is logged in” (the outermost describe) in your brain stack
- Then find and put “and is a manager” in your brain stack
- Then find and put “with multiple accounts” in your brain stack
- Remove “with multiple accounts” from your brain stack (because that context has ended without reaching your test)
- Then find and put “with a single account” in your brain stack
- Found the test
It is such a hassle.
The describe is there to describe what your test is doing. Be descriptive.