groovy / grails / unit testing / createCriteria.get

后端 未结 3 1012
無奈伤痛
無奈伤痛 2021-02-08 11:20

I can mock calls to:

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

with:

<         


        
3条回答
  •  无人共我
    2021-02-08 11:43

    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.

提交回复
热议问题