我对groovy很新。我正在尝试为我的groovy类编写测试用例,但由于我不知道从哪里开始,所以无法这样做。
我有一个常规课,如下所示。任何人都可以让我知道测试类应该怎么样?如果有相同的好教程?
试图通过谷歌查找信息,但无法找到很多帮助。感谢
class AddressController {
def index = { redirect(action:list,params:params) }
static allowedMethods = [delete:'POST', save:'POST', update:'POST']
def list = {
params.max = Math.min( params.max ? params.max.toInteger() : 10, 100)
[ addressInstanceList: Address.list( params ), addressInstanceTotal: Address.count() ]
}
def show = {
def addressInstance = Address.get( params.id )
if(!addressInstance) {
flash.message = "Address not found with id ${params.id}"
redirect(action:list)
}
else { return [ addressInstance : addressInstance ] }
}
def update = {
def addressInstance = Address.get( params.id )
if(addressInstance) {
if(params.version) {
def version = params.version.toLong()
if(addressInstance.version > version) {
addressInstance.errors.rejectValue("version", "address.optimistic.locking.failure", "Another user has updated this Address while you were editing.")
render(view:'edit',model:[addressInstance:addressInstance])
return
}
}
addressInstance.properties = params
if(!addressInstance.hasErrors() && addressInstance.save()) {
flash.message = "Address ${params.id} updated"
redirect(action:show,id:addressInstance.id)
}
else {
render(view:'edit',model:[addressInstance:addressInstance])
}
}
else {
flash.message = "Address not found with id ${params.id}"
redirect(action:list)
}
}
}
答案 0 :(得分:1)
您向我们展示的代码是Grails框架中的控制器类。我认为这是2.x版本(甚至更旧)。您可以在application.properties
属性下的app.grails.version
文件中查看。
当你知道Grails框架的实际版本是什么时,我建议从阅读有关单元测试的官方文档开始(http://docs.grails.org/2.5.6/guide/testing.html#unitTesting - 这里是描述版本2.5.6的单元测试的章节。)
您的控制器代表常规CRUD(Create-Read-Update-Delete的首字母缩写)操作。您可以测试多种情况:
仔细阅读章节"单元测试控制器"您可以在其中找到Grails中测试控制器的几个示例。请记住,如果您决定使用GORM域对象的单元测试控制器,则必须模拟每个域对象,并在测试用例类中添加@Mock([YourDomainClassName])
注释(这里详细描述:{{3 }})