RSpec is full of great features, but some of them either not very well documented or hidden to a naked eye. No wonder if you never discovered hierarchical specifications.
Traditionally, we write specifications like this:
1 2 3 4 5 6 7 | |
Now imagine that you have some very similar specs with some common initialization:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
You can see that you have common initializations for examples from both specifications and specific initializations for examples from each.
In the previous post I wrote that I prefer not to define helper methods outside the specification definition as they become global and tend to intersect with methods defined in other specs. So, what is the solution?
Assuming that you have a method “authenticate” that you need to call before every example in several specifications, you can define an upper-level specification “authenticated” and group other specs, like this:
1 2 3 4 5 6 7 8 9 10 11 12 | |
Now you have it all in a safe way!