I have the following Spock test, which passes:
def "test"() {
given:
def name = "John"
expect:
name.length() == 4
when:
name = name.concat(name)
then:
name.length() == 8
}
But when I modify the last then
block and make it an expect
block...
// previous part same
expect:
name.length() == 8
I am getting:
Groovy-Eclipse: Groovy:'expect' is not allowed here; instead, use one of: [and, then]
Is it because multiple expect
blocks are not allowed in a single test? If so, is this documented anywhere? There is a similar test here written with given - expect - when - then
but it is not clear why a second expect
was not used, although what is being asserted is same, just being flipped.
when-expect
is simply a syntax error with regard to Spock's specification DSL. The compiler message already tells you how to solve your problem. After when
you need then
(or and
first, if you want to structure your when
block into multiple sections). In contrast, expect
is a kind of when-then
contracted into a single block, because both the stimulus and verifying the response in a condition appear together. Block labels and how to use them is documented here.
Under Specifications as Documentation, you learn more about why you might want to use and
and block labels. Under Invocation Order you learn about what you can achieve by using multiple then
blocks in contrast to then-and
.
You can use multiple expect
blocks within one feature, no problem. But you do need to make sure that your when-then
(if any) is complete, before you can use another expect
.
For example, this is a valid feature:
def "my feature"() {
given: true
and: true
and: true
expect: true
when: true
and: true
then: true
and: true
then: true
expect: true
and: true
cleanup: true
}