How to use expect block in combination with each block

34 views
Skip to first unread message

Marcel Overdijk

unread,
Jan 7, 2013, 6:02:01 AM1/7/13
to Spock Framework - User
I'm creating a Spock test for a Gradle plugin and I wonder how I can
use the expect block in combination with a each closure like:

def "configures fail on version conflict resolution strategy for
each configuration"() {
def configurations = project.configurations.all

expect:
configurations.each { configuration ->
configuration.resolutionStrategy.conflictResolution
instanceof StringBuffer
}
}

The above code runs, but it does not the fail the test (which should
have been the case).

I changed it to:

def "configures fail on version conflict resolution strategy for
each configuration"() {
def configurations = project.configurations.all

expect:
configurations.each { configuration ->
assert configuration.resolutionStrategy.conflictResolution
instanceof StringBuffer
}
}

by adding the assert keyword and the test fails as expected. Note that
I still need the expect block definition.

Some questions:
1) Are the above assumptions correct, or am I missing an easier way?
2) More importantly, as the first code block was not failing the test
this could lead to some potential bugs. Is there some way to guard me
for this in Spock?


Cheers,
Marcel

Peter Niederwieser

unread,
Jan 7, 2013, 6:17:55 AM1/7/13
to spockfr...@googlegroups.com
Only top-level expressions are automatically considered assertions (anything else would cause troubles.) If you want to turn a nested expression into an assertion, you'll have to use `assert` explicitly. There is no way for Spock to automatically figure out your intent. Alternatively, you can use `every` instead of `each`.

Cheers,
Peter
> --
> You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
> To post to this group, send email to spockfr...@googlegroups.com.
> To unsubscribe from this group, send email to spockframewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.
>

Marcel Overdijk

unread,
Jan 7, 2013, 7:23:27 AM1/7/13
to Spock Framework - User
Thanks Peter,

I can understand there is no way Spock can figure out my intentions.
I tried 'every' as you suggested and it works. Only it does not
display the 'nested' debug info when a test fails, so for that reason
I will use assert explicitly.

My only worry is to forget about stating the explicit expectation.
Would it be an idea that Spock guards users in case you define expect
block but non assert would have been executed (both explicit/
implicit)?

On 7 jan, 12:17, Peter Niederwieser <pnied...@gmail.com> wrote:
> Only top-level expressions are automatically considered assertions (anything else would cause troubles.) If you want to turn a nested expression into an assertion, you'll have to use `assert` explicitly. There is no way for Spock to automatically figure out your intent. Alternatively, you can use `every` instead of `each`.
>
> Cheers,
> Peter
>

Peter Niederwieser

unread,
Jan 7, 2013, 7:31:25 AM1/7/13
to spockfr...@googlegroups.com
That's an interesting idea. It would (conceptually) work for expect-blocks, but not for then-blocks (which can additionally contain mock expectations).

Cheers,
Peter
Reply all
Reply to author
Forward
0 new messages