Skip to content

Instantly share code, notes, and snippets.

@imayobrown
Last active August 15, 2018 22:46
Show Gist options
  • Save imayobrown/daa0fab50a1a971b451b7f2435462ccc to your computer and use it in GitHub Desktop.
Save imayobrown/daa0fab50a1a971b451b7f2435462ccc to your computer and use it in GitHub Desktop.
Testing format example (no subfunctions)
#!groovy
package tests
import support.PipelineSpockTestBase
class TestThing extends PipelineSpockTestBase {
def scriptPath = 'vars/thing.groovy'
def script = null
def setup() {
this.script = loadScript(scriptPath)
}
def 'test thing'() {
given:
Closure mockWithStuff = Mock()
Closure mockLeaveWithStuff = Mock()
Closure mockDoTheThing = Mock()
helper.registerAllowedMethod('doTheThing', [Map.class], mockDoTheThing)
helper.registerAllowedMethod('withStuff', [Map.class], { args ->
mockWithStuff(args)
args.block()
mockLeaveWithStuff()
})
when:
script()
then:
1 * mockWithStuff({ mapArgs ->
mapArgs.arg1 == "arg"
})
then:
1 * mockDoTheThing({ mapArgs ->
mapArgs.foo == "foo" &&
mapArgs.bar == "bar"
})
then:
1 * mockLeaveWithStuff()
}
}
#!groovy
def call(Map args) {
withStuff arg1: "arg", block: {
doTheThing foo: "foo", bar: "bar"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment