Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Condition in expect block

Tags:

groovy

spock

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.

like image 325
kaskelotti Avatar asked Aug 22 '15 15:08

kaskelotti


1 Answers

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.

like image 65
Aseem Bansal Avatar answered Sep 30 '22 10:09

Aseem Bansal