Skip to content

Instantly share code, notes, and snippets.

@gregopet
Last active December 15, 2015 11:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregopet/7aedd77f4cf638fa31a9 to your computer and use it in GitHub Desktop.
Save gregopet/7aedd77f4cf638fa31a9 to your computer and use it in GitHub Desktop.
A way of testing closures in Grails
def action() {
mailService.sendMail emailParameters(params.mailTo, params.subject, params.body, attachments)
}
protected Closure emailParameters(List<String> mailTo, String mailSubject, String mailBody, List attachments) {
return {
multipart true
to mailTo
subject mailSubject
body mailBody
attachments.each {
attachBytes (it.filename, it.mime, it.bytes)
}
} as Closure
}
def ".emailParameters creates a configuration closure with the right properties set"() {
given:
def closure = controller.emailParameters(emailTo, emailSubject, emailBody, attachments)
def closureDelegate = Mock(Closure)
closure.delegate = closureDelegate
when:
closure()
then:
1*closureDelegate.invokeMethod('multipart', [true])
where:
emailBody = "body"
emailSubject = "subject"
emailTo = ["somebody@example.org"]
attachments = []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment