【发布时间】:2017-08-21 17:45:59
【问题描述】:
我有一个 Grails 服务,它会像这样执行 where 查询:
List<Car> search(Long makeId = null) {
Car.where {
join("make")
if(makeId) {
make.id == makeId
}
}.findAll()
}
我正在尝试像这样使用 Spock 对其进行单元测试:
def setup() {
GroovyMock(Car, global: true)
}
void "test search"() {
when:
service.search()
then:
1 * Car.where {}
}
但是,我似乎找不到测试闭包内容的方法。
我可以通过验证1 * Car.where(_) 来使测试通过,但是我如何才能对闭包的内容做出断言,即调用了join 并且仅在需要时指定了make.id 约束?
【问题讨论】:
-
我的偏好是测试搜索方法。在规范中,您将设置仅在指定 makeId 时才返回的数据。因此,使用两个“when / then”块,您可以测试提供/不提供 makeId 是否按预期工作。
标签: unit-testing grails closures spock