How to describe many contexts in ExUnit without a hierarchy

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:

  1. You will find the test and try to find the outer most describe
  2. Put “user is logged in” (the outermost describe) in your brain stack
  3. Then find and put “and is a manager” in your brain stack
  4. Then find and put “with multiple accounts” in your brain stack
  5. Remove “with multiple accounts” from your brain stack (because that context has ended without reaching your test)
  6. Then find and put “with a single account” in your brain stack
  7. Found the test

It is such a hassle.

The describe is there to describe what your test is doing. Be descriptive.

43 Likes