Unit testing 生成Grails 2单元测试,并在';t按预期运行并产生运行时错误
我有一个域类Unit testing 生成Grails 2单元测试,并在';t按预期运行并产生运行时错误,unit-testing,grails,groovy,Unit Testing,Grails,Groovy,我有一个域类 class File { String name String path static constraints = { name nullable:false path nullable:false } } 生成的控制器 class FileController { static allowedMethods = [create: ['GET', 'POST'], edit: ['GET', 'POST'], d
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)
因此,基本上没有名为update的控制器方法,因为用于编辑和更新的控制器已经组合在一起,并使用request.method='POST'或'GET'暂停适当的操作
所以基本上我的问题是如何生成正确的单元测试用例?还是我完全错过了什么
我使用的是Grails2.1.1这里的代码看起来像锅炉板脚手架模板,所以我假设您在某个时候针对文件域类运行了generate all。这样做可能会创建您的FileController和相应的FileControllerTest,其中包含所有“crud”操作/测试 这里只是猜测一下,但在某个时候,您可能已经删除了FileController中的更新操作,但保留了FileControllerTest,这就给您留下了
//in FileControllerTests
void testUpdate() {
controller.update()
...
}
只要更新您的测试类-删除testUpdate,或者如果您认为以后需要它,请将其注释掉。因此,当我使用twitter引导脚手架文件更新模板文件时,出现了这种差异。他们在脚手架源代码中没有更新的Test.groovy文件
我必须更新Test.groovy文件以对应新的Controller.groovy文件。您发布的控制器代码中没有更新方法。这正是异常告诉您的。是的,我看到了,但我想知道为什么控制器没有生成正确的测试。是的,测试和控制器支架类不在接收器中。我不知道他们是如何从水槽中出来的,我也找不到和更新后的控制器一致的测试代码。我没有在FileController中删除我的更新。现在动作由edit定义,在edit中有一个switch语句,它结合了旧的edit/update功能。我想也许我只需要一个指向相应Test.groovy模板代码的链接。然而,如果有人知道更多关于这个问题,请分享。