繁体   English   中英

Grails Spock单元测试查询

[英]Grails spock unit testing query

我在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查询进行嘲笑,以便任何人都知道任何问题吗? TIA ..

通过此代码,您正在嘲笑(或添加)方法

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()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM