Condition in expect block
Asked Answered
H

1

7

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.

Hahn answered 22/8, 2015 at 15:16 Comment(5)
Not answering the question, but why would you test like this? Shouldn't you be testing known things? If there are different paths, shouldn't they be different tests?Offenbach
Seconding what tim said. If the test fails would you be able to tell what exactly failed instead of one of X things failed? Perhaps this was just an example but every test should be testing one things only. Perhaps you need similar test with some values changed? If that is the case then you should consider looking at where statement in spock.Outreach
@Offenbach @Aseem Reason for using an if statement when checking for conditions is because of complexity of test input and that only a few exceptional cases fall into the other branch of if, which basically boils down to laziness :) The use case is something like Check that condition A is met unless X, in which case check that condition B is met instead. Could be solved with a where block, but for my taste it's too verbose.Hahn
So why not just test for condition A being met, then have another test for the case where X must occur?Offenbach
@Offenbach Since there's a big init block for the test data which will be used elsewhere and for this specific case there are certain parts that are to be skipped. I appreciate you and your input on this, but it's getting a bit off topic. I'll ponder on this a bit, and open a new question if seen necessary. Thanks.Hahn
O
9

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.

Outreach answered 22/8, 2015 at 15:29 Comment(3)
Yes, it states that all top-level expressions in these blocks are implicitly treated as conditions. If in the above example the if block is replaced by ternary operator, no explicit condition is required with assert keyword. Similar to having a helper method do the condition checking, one needs explicit asserts in if statement. Basically I wish to know is there a link between the two, i.e. is if statement actually a function call in Groovy with void as a return.Hahn
No, it isn't. It's the same as JavaOffenbach
For future reference you can check groovy-lang.org/differences.html for finding whether something works differently in groovy compared to Java.Outreach

© 2022 - 2024 — McMap. All rights reserved.