Spock Web Console

subscribe to the feed Subscribe
Sometimes You Need Asserts (1) (via #spockwebconsole)
tweet this script Tweet

Sometimes You Need Asserts (1)

Published 5 months ago by Michael Delaney with tags spock assert
Actions  ➤ Edit In Console Back To Console Show/Hide Line Numbers View Recent Scripts
import spock.lang.*

class SometimesYouNeedAsserts extends Specification {
    class Fakey {
        String val
        String val2
    }

    def "Be very careful with _each_ closures in Spock! You need asserts!"() {
        given: "An array of Strings"
        def strings = [new Fakey(val:'foo', val2:'foo2'), new Fakey(val:'foo', val2:'foo2'), new Fakey(val:'foo', val2:'foo2')]
    
        expect: "This will pass - and it shouldn't"
        strings.each {
            it.val == 'oops'
        }
    }

    def "When we put asserts into the expect, the test will (correctly) fail"() {
        given: "Same as before"
        def strings = [new Fakey(val:'foo', val2:'foo2'), new Fakey(val:'foo', val2:'foo2'), new Fakey(val:'foo', val2:'foo2')]

        expect: "You need to use asserts if you want to use _each_. The below will correctly fail."
        strings.each {
            assert it.val == 'blah'
        }
    }
}