grails中单元测试中的@Mock域对象listOrderBy动态查找器抛出异常

时间:2012-11-10 00:57:29

标签: unit-testing grails mocking dynamic-finders

我正在为grails控制器编写单元测试。以下是代码片段:

@TestFor(MyController)
@Mock([MyDomain])
class MyControllerTests {

    void testController() {
        ...
        ...
    }
}

以下是域对象的外观:

class MyDomain {
    static constraints = {
        name(nullable: false)
        parent(nullable: true)
    }

    static belongsTo = Industry

    static hasMany = [children: Industry]

    Industry parent
    String name
}

我正在测试的控制器中的方法调用此GORM动态方法:

MyDomain.listOrderByParent()

当执行命中此行时测试失败,并且异常对我没有多大意义,因为@Mock注释应该添加了所有动态方法:

groovy.lang.GroovyRuntimeException: Cannot compare com.stuff.MyDomain with value 'com.stuff.MyDomain : 1' and com.stuff.MyDomain with value 'com.stuff.MyDomain : 4'
at org.grails.datastore.mapping.simple.query.SimpleMapQuery$_executeQuery_closure63_closure155.doCall(SimpleMapQuery.groovy:78)

运行grails应用程序时控制器正常工作。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以模拟Industry域对象  以及:

@Mock([MyDomain, Industry])