groovy / grails / unit testing / createCriteria.get [英] groovy / grails / unit testing / createCriteria.get

查看:100
本文介绍了groovy / grails / unit testing / createCriteria.get的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以模拟调用:

  MyDomainClass.createCriteria()。list {
eq('id' ,id)
eq('anotherParameter',anotherParameterId)
}

用:

  def myCriteria = [
list:{Closure cls - > returnThisObject}
]
MyDomainClass.metaClass.static.createCriteria = {myCriteria}

建议:

http://davistechyinfo.blogspot.com/2010/01/mocking-hibernate-criteria-in-grails.html



但用于:

  MyDomainClass.createCriteria()。get {
eq('id',id)
eq('anotherParameter',anotherParameterId)
}

这种方法失败 - 可能是因为'get'是一种关键词,'list'不是。任何人都可以建议 - 能够在域类中嘲笑这一点应该是可能的,而不必简​​单地放弃使用 createCriteria()。get {} 的方法的单元测试覆盖。 >

建议非常感谢,

Alex

解决方案

  def> 

myCriteria = new Expando();
myCriteria .get = {Closure cls - > returnThisObject}
MyDomainClass.metaClass.static.createCriteria = {myCriteria}

这正是我想要的,并且可能支持测试提供的参数。感谢您的其他回复。希望这对其他人测试domain / createCriteria()方法有用。


I can mock calls to:

MyDomainClass.createCriteria().list{
    eq('id',id)
    eq('anotherParameter',anotherParameterId)
}

with:

def myCriteria = [
    list : {Closure  cls -> returnThisObject}
]
MyDomainClass.metaClass.static.createCriteria = { myCriteria }

as advised at:

http://davistechyinfo.blogspot.com/2010/01/mocking-hibernate-criteria-in-grails.html

but for:

MyDomainClass.createCriteria().get{
    eq('id',id)
    eq('anotherParameter',anotherParameterId)
}

This approach fails - maybe because 'get' is a keyword in a way 'list' is not. Can anyone advise - being able to mock this in domain classes should be possible, without simply abandoning unit test coverage for methods that use createCriteria().get{}.

Suggestions much appreciated,

Alex

解决方案

I've found a solution that doesn't compromise my ability to write unit tests -

def myCriteria = new Expando();
myCriteria .get = {Closure  cls -> returnThisObject}         
MyDomainClass.metaClass.static.createCriteria = {myCriteria }

which does exactly what I wanted and potentially supports testing supplied arguments. Thanks for the other response. Hope this is useful to others testing domain/createCriteria() methods.

这篇关于groovy / grails / unit testing / createCriteria.get的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆