我在Spock单元测试中使用grails 2.3.7 ..我在模拟findWhere方法..
DocumentHeader.metaClass.static.findWhere = {DocumentType type, String workStation, Boolean complete ->
println "Running mock findWhere .. "
new DefaultDocument()
}
我用来模拟服务中的方法调用的..
def returnDocument = DocumentHeader.findWhere(documentType:DocumentType.DEFAULT_TYPE,
workStation: requirement.workstation,
complete: false)
参数类型正确,但是在调用测试时得到
Cannot query [com.sample.DocumentHeader] on non-existent property: workStation org.springframework.dao.InvalidDataAccessResourceUsageException: Cannot query [com.vantec.DocumentHeader] on non-existent property: workStation
at org.grails.datastore.mapping.simple.query.SimpleMapQuery
所以它似乎是在调用真正的方法-而不是模拟方法。不要回想起之前对模拟findWhere查询的 mock ,这样任何人都知道任何问题吗? TIA ..
最佳答案
通过此代码,您正在 mock (或添加)方法
DocumentHeader findWhere(DocumentType dt, String w, Boolean c)
但在您服务中,您正在打电话
DocumentHeader findWhere(Map props)
尝试将您的metaClass更改为:
DocumentHeader.metaClass.static.findWhere = {Map props ->
println "Running mock findWhere .. "
new DefaultDocument()
}
关于unit-testing - Grails Spock单元测试查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35791261/