kaskelotti
kaskelotti

Reputation: 4832

Condition in expect block

I just noticed that Spock does not assert the conditions if I add an if clause in the expect block as in

def myTest() {
  given:
    a = true

  expect:
    if ( a ) {
      1 == 2
    }
    else {
      1 == 1
    }      
}

The above test will pass since the conditions are not checked. Or the condition checking does not get forwarded pass the if statement.

The workaround to this is to add assert statements inside the if block, i.e. assert 1 == 2.

What I'm interested is, is why the functionality is like this? Is there some other way to workaround this? I'm assuming this has something to do with Groovy if statement functionality, but I don't know the language details well enough. Most probably the if statement does not return anything for the Spock's expect block to work with.

Upvotes: 7

Views: 6393

Answers (1)

Aseem Bansal
Aseem Bansal

Reputation: 6972

This has nothing to do with groovy. Spock's documentation clearly states that only top-level expressions are considered in then and expect as conditions. It is by design.

Search the link for top.

Upvotes: 9

Related Questions