assertion.should be_related_to_description —
When you are writing positive specs, use positive assertions. When you are writing negative specs, use negative assertions. This might sound trivial, but a lot of people are missing out on this.
Let’s look at a simple example. This simple User class has a method called ‘visit’, and a counter for unique visited attractions.
We want to verify that visiting a new attraction increases the visited attractions count, but multiple visits to the same place does not increase the count.
The problem is here subtle. The second spec is negative (it ”doesn’t increase…”), but the assertion is positive. A better way to write this would be:
Why is this better? It might look the same when you are writing the spec, but when the spec breakes and you want to understand what went wrong, I find it easier to know that what I see in the name of the spec is not happening, instead of doing a double not operator in my head.
This is quite similar to the “it ‘should foo‘” vs. “it ‘foos‘” debate – again, I find it easier to look at spec names and see what happens, instead of what should happen.
Categorised as: Uncategorized