Recently I've been involved on some work on a Grails app, for which specifications are implemented using spock framework. It's a very readable and solid spec framework, which I also highly recommend. One way of writing spock specification is using some kind of BDD-style constraints, like:
As you can see, expression written in the
then: block are treated as asserts, and your test will fail if one of the conditions is false.
Sometimes, we need to assert more conditions, and for better readability of our tests we use some private methods, like:
The subtle thing here is that, since we're not in the spock dsl anymore, conditions in private methods are not treated automatically as asserts, and we need to add the
assert keyword in order to have our test working properly.
After discovering that we had to fix a bunch of tests that were written incorrectly...
This also leads to another key concept - always make sure that your test fails for the reason you're expecting it to fail. While doing TDD this comes at the very beginning of the process, if you're not applying TDD you have to change your code accordingly to make your test fail.