On Github thecodesmith / spock-talk
Master programmers think of systems as stories to be told rather than programs to be written.
-- Robert C. Martin: Clean Code
import spock.lang.Specification class SimpleSpec extends Specification { def 'maximum of two numbers'() { expect: Math.max(1, 3) == 4 } }
SimpleSpec - maximum of two numbers FAILED Condition not satisfied: Math.max(1, 3) == 4 | | 3 false at SimpleSpec.maximum of two numbers(Script1.groovy:6)
given: / when: / then:
class SimpleSpec extends Specification { def 'maximum value of list'() { given: def values = [4, 7, 9, 5] when: def max = values.max() then: max == 9 } }
setup: / expect: / where:
import groovy.sql.Sql class SimpleSpec extends Specification { def 'sorted crew names'() { setup: def starship = new Expando(name: 'Enterprise', captain: name) expect: starship.captain == name where: name << ['Kirk', 'Picard'] } }
class StateSpec extends Specification { Car car def setupSpec() { car = new Car(make: 'Toyota', model: 'Corolla') } def setup() { car.mileage = 20000 } }
def 'add item to collection'() { given: 'a list of values' def values = [4, 8, 15, 16] when: 'adding a single value' values << 23 then: 'list size increases by 1' values.size() == old(values.size()) + 1 }
def 'maximum of two numbers'() { expect: Math.max(a, b) == c where: a | b || c 1 | 3 || 3 7 | 4 || 7 0 | 0 || 0 }
def 'maximum of two numbers'() { ... where: name << ['kirk', 'spock', 'scotty'] }
@Unroll def 'maximum of two numbers'() { expect: Math.max(a, b) == c where: a | b || c 1 | 3 || 3 7 | 3 || 3 0 | 0 || 0 }
Test iterations reported separately:
maximum of two numbers[0] PASSED maximum of two numbers[1] FAILED Math.max(a, b) == c | | | | | | 7 3 | 3 7 false maximum of two numbers[2] PASSED
class Publisher { List<Subscriber> subscribers void send(String message) } interface Subscriber { void receive(String message) } class PublisherSpec extends Specification { Publisher publisher = new Publisher() }
class PublisherSpec extends Specification { Publisher publisher = new Publisher() Subscriber subscriber = Mock() Subscriber subscriber2 = Mock() def setup() { publisher.subscribers << subscriber // << is a Groovy shorthand for List.add() publisher.subscribers << subscriber2 } }
def "should send messages to all subscribers"() { when: publisher.send("hello") then: 1 * subscriber.receive("hello") 1 * subscriber2.receive("hello") }
Declaring Cardinality
1 * subscriber.receive("hello") // exactly one call 0 * subscriber.receive("hello") // zero calls (1..3) * subscriber.receive("hello") // between one and three calls (inclusive) (1.._) * subscriber.receive("hello") // at least one call (_..3) * subscriber.receive("hello") // at most three calls _ * subscriber.receive("hello") // any number of calls, including zero
Argument Constraints
1 * subscriber.receive("hello") // an argument that is equal to the String "hello" 1 * subscriber.receive(!"hello") // an argument that is unequal to the String "hello" 1 * subscriber.receive() // the empty argument list 1 * subscriber.receive(_) // any single argument (including null) 1 * subscriber.receive(*_) // any argument list (including empty list) 1 * subscriber.receive(!null) // any non-null argument 1 * subscriber.receive(_ as String) // any non-null argument that is-a String 1 * subscriber.receive({ it.size() > 3 }) // an argument that satisfies given predicate // (here: message length is greater than 3)