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 assert each
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 "This test will pass because fakeys is not false"() {
        given: "An array of Fakeys"
        def fakeys = [new Fakey(val:'foo'), new Fakey(val:'foo'), new Fakey(val:'foo')]
    
        expect: "This will pass - and it shouldn't"
        fakeys.each {
            it.val == 'oops'
        }
    }

    def "When we put asserts into the expect, the test will (correctly) fail"() {
        given: "A similar array of Fakeys"
        def fakeys = [new Fakey(val:'foo'), new Fakey(val:'foo'), new Fakey(val:'foo')]

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