Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing Grails2.4.4生成的控制器测试失败_Unit Testing_Grails - Fatal编程技术网

Unit testing Grails2.4.4生成的控制器测试失败

Unit testing Grails2.4.4生成的控制器测试失败,unit-testing,grails,Unit Testing,Grails,我对控制器的保存操作进行了测试。它只是使用正确的参数执行操作,但问题出在redirectedUrl行上:它为null 使用该应用程序,在保存域实例后,我将重定向到show操作,并正确呈现show视图 这里有什么问题的线索吗 控制员: @Transactional(readOnly = true) class FolderController { static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

我对控制器的保存操作进行了测试。它只是使用正确的参数执行操作,但问题出在redirectedUrl行上:它为null

使用该应用程序,在保存域实例后,我将重定向到show操作,并正确呈现show视图

这里有什么问题的线索吗

控制员:

@Transactional(readOnly = true)
class FolderController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
    ...

    @Transactional
    def save(Folder folderInstance) {

        if (folderInstance == null) {
            notFound()
            return
        }

        if (folderInstance.ehrId)
        {
           def ehr = ehr.Ehr.get(folderInstance.ehrId)
           ehr.directory = folderInstance
           ehr.save() 
        }

        if (folderInstance.hasErrors()) {
            respond folderInstance.errors, view:'create'
            return
        }

        folderInstance.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'folder.label', default: 'Folder'), folderInstance.id])
                redirect folderInstance
            }
            '*' { respond folderInstance, [status: CREATED] }
        }
    }
    ...
}
测试:

@TestFor(FolderController)
@Mock(Folder)
class FolderControllerSpec extends Specification {

        ...
    void "Test the save action correctly persists an instance"() {

        when:"The save action is executed with a valid instance"
            response.reset()
            populateValidParams(params)
            def folder = new Folder(params)

            controller.save(folder)
            println folder.errors // no errors

        then:"A redirect is issued to the show action"
            response.redirectedUrl == '/folder/show/1'
            controller.flash.message != null
            Folder.count() == 1
    }
    ...
}
输出:

junit.framework.AssertionFailedError: Condition not satisfied:

response.redirectedUrl == '/folder/show/1'
|        |             |
|        null          false
org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletResponse@112b2f1

    at directory.FolderControllerSpec.Test the save action correctly persists an instance(FolderControllerSpec.groovy:61)

Grailsscaffold控制器是更智能的控制器。它们尊重请求格式并相应地生成响应

例如,您的保存操作——如果请求格式为
form
,它将重定向到show操作,否则它将返回状态为
CREATED
的已保存域实例

以下代码对此负责

request.withFormat {
    form multipartForm {
        flash.message = message(code: 'default.created.message', args: [message(code: 'folder.label', default: 'Folder'), folderInstance.id])
        redirect folderInstance
    }
    '*' { respond folderInstance, [status: CREATED] }
}
在您的测试用例中,您的请求不是
表单
类型,因此
redirectedUrl
为空

要发出
表单
请求,请在进行save调用之前在测试用例中添加以下代码--


希望这有帮助。

我忘了添加allowedMethods字段

第一个问题是生成的测试没有为相应的操作设置正确的请求方法,因此需要调用.save():controller.request.method=“POST”

然后@user1690588所建议的(request.format='form')实现了获得正确重定向URL的技巧

我的最终测试如下所示:

void "Test the save action correctly persists an instance"() {

    when:"The save action is executed with a valid instance"
        response.reset()
        populateValidParams(params)
        def folder = new Folder(params)

        controller.request.method = "POST"
        request.format = 'form'

        controller.save(folder)

    then:"A redirect is issued to the show action"
        response.redirectedUrl == '/folder/show/1'
        controller.flash.message != null
        Folder.count() == 1
}

正在审阅代码,但这不是脚手架控制器,因为它没有脚手架属性:。最初的问题是Grails没有在测试中生成正确的请求方法,当我解决了这个问题时,您关于格式的建议起到了作用。我希望Grails能够在测试中生成正确的请求方法和格式,但在生成之后,它仍然需要一些手动工作。
void "Test the save action correctly persists an instance"() {

    when:"The save action is executed with a valid instance"
        response.reset()
        populateValidParams(params)
        def folder = new Folder(params)

        controller.request.method = "POST"
        request.format = 'form'

        controller.save(folder)

    then:"A redirect is issued to the show action"
        response.redirectedUrl == '/folder/show/1'
        controller.flash.message != null
        Folder.count() == 1
}