我有一个Domain类
class File {
String name
String path
static constraints = {
name nullable:false
path nullable:false
}
}
生成的控制器
class FileController {
static allowedMethods = [create: ['GET', 'POST'], edit: ['GET', 'POST'], delete: 'POST']
def index() {
redirect action: 'list', params: params
}
def list() {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
[fileInstanceList: File.list(params), fileInstanceTotal: File.count()]
}
def create() {
switch (request.method) {
case 'GET':
[fileInstance: new File(params)]
break
case 'POST':
def fileInstance = new File(params)
if (!fileInstance.save(flush: true)) {
render view: 'create', model: [fileInstance: fileInstance]
return
}
flash.message = message(code: 'default.created.message', args: [message(code: 'file.label', default: 'File'), fileInstance.id])
redirect action: 'show', id: fileInstance.id
break
}
}
def show() {
def fileInstance = File.get(params.id)
if (!fileInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'file.label', default: 'File'), params.id])
redirect action: 'list'
return
}
[fileInstance: fileInstance]
}
def edit() {
switch (request.method) {
case 'GET':
def fileInstance = File.get(params.id)
if (!fileInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'file.label', default: 'File'), params.id])
redirect action: 'list'
return
}
[fileInstance: fileInstance]
break
case 'POST':
def fileInstance = File.get(params.id)
if (!fileInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'file.label', default: 'File'), params.id])
redirect action: 'list'
return
}
if (params.version) {
def version = params.version.toLong()
if (fileInstance.version > version) {
fileInstance.errors.rejectValue('version', 'default.optimistic.locking.failure',
[message(code: 'file.label', default: 'File')] as Object[],
"Another user has updated this File while you were editing")
render view: 'edit', model: [fileInstance: fileInstance]
return
}
}
fileInstance.properties = params
if (!fileInstance.save(flush: true)) {
render view: 'edit', model: [fileInstance: fileInstance]
return
}
flash.message = message(code: 'default.updated.message', args: [message(code: 'file.label', default: 'File'), fileInstance.id])
redirect action: 'show', id: fileInstance.id
break
}
}
def delete() {
def fileInstance = File.get(params.id)
if (!fileInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'file.label', default: 'File'), params.id])
redirect action: 'list'
return
}
try {
fileInstance.delete(flush: true)
flash.message = message(code: 'default.deleted.message', args: [message(code: 'file.label', default: 'File'), params.id])
redirect action: 'list'
}
catch (DataIntegrityViolationException e) {
flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'file.label', default: 'File'), params.id])
redirect action: 'show', id: params.id
}
}
和生成的测试用例
@TestFor(FileController)
@Mock(File)
class FileControllerTests {
def populateValidParams(params) {
assert params != null
params["name"] = 'someValidName'
params["path"] = 'someValidPath'
}
void testIndex() {
controller.index()
assert "/file/list" == response.redirectedUrl
}
void testList() {
def model = controller.list()
assert model.fileInstanceList.size() == 0
assert model.fileInstanceTotal == 0
}
void testCreate() {
def model = controller.create()
assert model.fileInstance != null
}
void testSave() {
controller.save()
assert model.fileInstance != null
assert view == '/file/create'
response.reset()
populateValidParams(params)
controller.save()
assert response.redirectedUrl == '/file/show/1'
assert controller.flash.message != null
assert File.count() == 1
}
void testShow() {
controller.show()
assert flash.message != null
assert response.redirectedUrl == '/file/list'
populateValidParams(params)
def file = new File(params)
assert file.save() != null
params.id = file.id
def model = controller.show()
assert model.fileInstance == file
}
void testEdit() {
controller.edit()
assert flash.message != null
assert response.redirectedUrl == '/file/list'
populateValidParams(params)
def file = new File(params)
assert file.save() != null
params.id = file.id
def model = controller.edit()
assert model.fileInstance == file
}
void testUpdate() {
controller.update()
assert flash.message != null
assert response.redirectedUrl == '/file/list'
response.reset()
populateValidParams(params)
def file = new File(params)
assert file.save() != null
// test invalid parameters in update
params.id = file.id
params["name"] = null
params["path"] = null
controller.update()
assert view == "/file/edit"
assert model.fileInstance != null
file.clearErrors()
populateValidParams(params)
controller.update()
assert response.redirectedUrl == "/file/show/$file.id"
assert flash.message != null
//test outdated version number
response.reset()
file.clearErrors()
populateValidParams(params)
params.id = file.id
params.version = -1
controller.update()
assert view == "/file/edit"
assert model.fileInstance != null
assert model.fileInstance.errors.getFieldError('version')
assert flash.message != null
}
void testDelete() {
controller.delete()
assert flash.message != null
assert response.redirectedUrl == '/file/list'
response.reset()
populateValidParams(params)
def file = new File(params)
assert file.save() != null
assert File.count() == 1
params.id = file.id
controller.delete()
assert File.count() == 0
assert File.get(file.id) == null
assert response.redirectedUrl == '/file/list'
}
}
我为测试用例的传递和失败添加了正确的参数,但仍然存在运行时错误,并且控制器中生成的代码与测试用例中生成的代码不对齐。测试用例调用控制器中不存在的方法。
这是错误代码/堆栈跟踪
groovy.lang.MissingMethodException: No signature of method: FileController.update() is applicable for argument types: () values: []
Possible solutions: create(), putAt(java.lang.String, java.lang.Object), delete(), edit(), isCase(java.lang.Object), split(groovy.lang.Closure)
at FileControllerTests.testUpdate(FileControllerTests.groovy:98)
因此,基本上没有称为更新的控制器方法,因为已经组合了用于编辑和更新日期的控制器并使用request.method =' POST'或者' GET'暂停适当的行动。
所以基本上我的问题是如何才能生成正确的单元测试用例?或者我完全错过了其他的东西?
我正在使用grails 2.1.1
答案 0 :(得分:1)
你在那里的代码看起来像锅炉板脚手架模板,所以我假设你在某些时候对File域类运行了generate-all。这样做可能会创建你的FileController和相应的FileControllerTest以及所有“crud”动作/测试。
这里只是猜测一下,但是在某些时候你可能已经删除了FileController中的更新操作但是保留了FileControllerTest,这样你就可以了
//in FileControllerTests
void testUpdate() {
controller.update()
...
}
只需更新您的测试类 - 如果您认为以后需要,请删除testUpdate或将其注释掉。
答案 1 :(得分:0)
因此,当我使用twitter bootstrap脚手架文件更新模板文件时,会发生这种差异。他们在脚手架源中没有更新的Test.groovy文件。
我必须更新Test.groovy文件以对应新的Controller.groovy文件。