我有一个Grails的服务,做了where
这样的查询:单元测试GORM调用与闭合
List<Car> search(Long makeId = null) {
Car.where {
join("make")
if(makeId) {
make.id == makeId
}
}.findAll()
}
我试图单元测试与斯波克这样的:
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。 – bassmartin