Spock Web Console

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

Sometimes You Need Asserts - 3

Published 1 month ago by Michael Delaney with tags each every 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 "This test will pass because fakeys is not false"() {
        given: "A list 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 list 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'
        }
    }

    def "Using every instead of each"() {
        given: "Our familiar list of Fakeys"
        def fakeys = [new Fakey(val:'foo'), new Fakey(val:'foo'), new Fakey(val:'foo')]
        
        expect: "Using every eliminates the need for assert"
        fakeys.every {
            it.val == 'oops'
        }
    }
}