2
我试图验证,在一类这个常规闭合称为CUTService有正确的价值观:试图验证的Groovy闭包在一个单元测试
mailService.sendMail {
to '[email protected]'
from '[email protected]'
subject 'Stuff'
body 'More stuff'
}
我已经看了https://github.com/craigatk/spock-mock-cheatsheet/raw/master/spock-mock-cheatsheet.pdf,但他的语法产生错误:
1 * mailService.sendMail({ Closure c -> c.to == '[email protected]'})
groovy.lang.MissingPropertyException: No such property: to for class: com...CUTService
我已经看了Is there any way to do mock argument capturing in Spock,并试图此:
1 * mailService.sendMail({closure -> captured = closure })
assertEquals '[email protected]', captured.to
主要生产:
groovy.lang.MissingPropertyException: No such property: to for class: com...CUTService
我也试过这样:
1 * mailService.sendMail({captured instanceof Closure })
assertEquals '[email protected]', captured.to
主要生产:
Too few invocations for:
1 * mailService.sendMail({captured instanceof Closure }) (0 invocations)
...
Unmatched invocations (ordered by similarity):
1 * mailService.sendMail([email protected])
什么我需要做的就是这个工作?
您的意思是'1 * mailService.sendMail'? Spec中没有sendMail。尽管如此,采用这种方法即使在CUT出错的情况下也能通过测试。 –
我只能向您展示您可以编写的测试类型以及它的工作原理,但是如果没有更多的上下文,您的课程等,我无法编写一份工作测试! ;-) –
它适用于您的编辑。谢谢! –