1
我正在爲grails控制器編寫單元測試。下面是代碼片段:@Mock域對象在單元測試grails listOrderBy動態查找引發異常
@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應用程序時工作正常。有任何想法嗎?
能否請您附上測試方法的實現? –