Sets the specified object as the implicit target of the top-level conditions and/or
interactions contained in the specified code block, thereby avoiding the need to repeat
the same expression multiple times. Implicit conditions are supported. (In other words,
the
assert keyword may be omitted.) If the target is
null, a
SpockAssertionError is thrown.
A
with block can be used anywhere in a spec, including nested positions
and helper methods.
Condition example:
def fred = new Person(name: "Fred", age: 42)
def spaceship = new Spaceship(pilot: fred)
expect:
with(spaceship.pilot) {
name == "Fred" // shorthand for: spaceship.pilot.name == "Fred"
age == 42
}
Interaction example:
def service = Mock(Service) // has start(), stop(), and doWork() methods
def app = new Application(service) // controls the lifecycle of the service
when:
app.run()
then:
with(service) {
1 * start() // shorthand for: 1 * service.start()
1 * doWork()
1 * stop()
}