I would like to test the Grails controller, which sends emails using the Grails Email plugin. I don’t understand how to mock with closure sendMailso that interactions work. Here is my latest version of the test code:
def 'controller should send a multipart email'() {
given: 'a mocked mailService'
controller.mailService = Mock(grails.plugin.mail.MailService)
controller.mailService.sendMail(*_) >> Mock(org.springframework.mail.MailMessage)
when:
controller.sendNow()
then:
1* _.multipart(true)
}
The controller code looks something like you expected, for example:
def mailService
def sendNow() {
mailService.sendMail {
multipart true
to 'example@example.org'
from 'me@here.com'
subject 'a subject'
body 'a body'
}
}
If I run this test, I get 0 calls of my interaction multipart, not 1. The second line of the block given:seems suspicious to me, but if I try to make fun of my test failures Closureinstead org.springframework.mail.MailMessage. I should also mention that the controller itself works as expected (it could not wait until I can parse the unit tests first).
Edited
, , , ; , multipart DSL, , sendMail ( , ). , sendMail, , .