控制器集成测试

发布于 2024-10-02 09:55:15 字数 4151 浏览 0 评论 0原文

控制器操作:

def deleteDept = {

        def departmentInstance = Department.findByName(params.department.name)

        if (!departmentInstance) {
            println "no dept instance"
            throw new org.codehaus.groovy.grails.exceptions.NewInstanceCreationException ("could not create DeptInstance for ${params.department.name}")
        } else if (departmentInstance.paySvcs && !departmentInstance.paySvcs.isEmpty()){
            println "instance with paySvcs"
            // !!!! do not delete the department if it has payment services !!!!
            departmentInstance.errors.reject('department.do.not.delete.message')
//            render(view: "editDept", model: [departmentInstance: departmentInstance])
            redirect(action: "editDept", id: departmentInstance.id)
        } else{
            println "proceed to delete"
            try {
                departmentInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'department.label', default: 'Department'), departmentInstance.name])}"
                redirect(action: "appAdmin", id: departmentInstance.id)
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                println "something went wrong"
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'department.label', default: 'Department'), departmentInstance.name])}"
                redirect(action: "editDept", id: departmentInstance.id)
            }
        }
    }

集成测试:

        def AppAdminController controller = new AppAdminController()        // create the controller
        controller.metaClass.message = { Map p -> return "foo" }            // message dummy returning garbage - work around the controller message exception
        controller.params.department = [name:"dept1", phone:"817-273-3260", g_password:"password", street:"Main St.", g_userID:"user", unitCode:"1234567", email:"[email protected]", zip:"75097", fax:"817-273-2222"]
        def dept2 = new Department (name: "Dept2", unitCode: "1234568", street: "Main St.", zip: "75097", fax: "817-273-2222", phone: "817-273 3260", email: "[email protected]", g_userID: "user", g_password: "password")
        def dept1 = new Department (name: "Dept1", unitCode: "1234568", street: "Main St.", zip: "75097", fax: "817-273-2222", phone: "817-273 3260", email: "[email protected]", g_userID: "user", g_password: "password")
        def math = new PaySvc()
        def another = new PaySvc()
        dept.paySvcs = []
        dept.paySvcs.add(math)
        dept.paySvcs.add(another)
        mockDomain(Department, [dept1, dept2])

        def svcTest = Department.findByName("Dept1")
        assertNotNull svcTest                                           // record Dept1 exists
        assertEquals 2, svcTest.paySvcs.size()                          // 2 paySvcs total

        controller.deleteDept()                                         // calling the action

        svcTest = null
        svcTest = Department.findByName("Dept1")
        assertNotNull svcTest                                           // record Dept1 still exists, it was not deleted
        // can't test render, controller.response.contentAsString is null
        // but testing at the UI is OK
 //       assertEquals "editDept", controller.response.contentAsString    // action should render "editDept"
        assertEquals "editDept", controller.redirectArgs.action

问题:

render(view: "editDept", model: [departmentInstance: departmentInstance]) 

在 UI 上手动测试时有效。但在集成测试中,使用render时controller.response为null,但使用redirect时返回预期的redirectArg。 有什么想法吗?

CONTROLLER ACTION:

def deleteDept = {

        def departmentInstance = Department.findByName(params.department.name)

        if (!departmentInstance) {
            println "no dept instance"
            throw new org.codehaus.groovy.grails.exceptions.NewInstanceCreationException ("could not create DeptInstance for ${params.department.name}")
        } else if (departmentInstance.paySvcs && !departmentInstance.paySvcs.isEmpty()){
            println "instance with paySvcs"
            // !!!! do not delete the department if it has payment services !!!!
            departmentInstance.errors.reject('department.do.not.delete.message')
//            render(view: "editDept", model: [departmentInstance: departmentInstance])
            redirect(action: "editDept", id: departmentInstance.id)
        } else{
            println "proceed to delete"
            try {
                departmentInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'department.label', default: 'Department'), departmentInstance.name])}"
                redirect(action: "appAdmin", id: departmentInstance.id)
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                println "something went wrong"
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'department.label', default: 'Department'), departmentInstance.name])}"
                redirect(action: "editDept", id: departmentInstance.id)
            }
        }
    }

INTEGRATION TEST:

        def AppAdminController controller = new AppAdminController()        // create the controller
        controller.metaClass.message = { Map p -> return "foo" }            // message dummy returning garbage - work around the controller message exception
        controller.params.department = [name:"dept1", phone:"817-273-3260", g_password:"password", street:"Main St.", g_userID:"user", unitCode:"1234567", email:"[email protected]", zip:"75097", fax:"817-273-2222"]
        def dept2 = new Department (name: "Dept2", unitCode: "1234568", street: "Main St.", zip: "75097", fax: "817-273-2222", phone: "817-273 3260", email: "[email protected]", g_userID: "user", g_password: "password")
        def dept1 = new Department (name: "Dept1", unitCode: "1234568", street: "Main St.", zip: "75097", fax: "817-273-2222", phone: "817-273 3260", email: "[email protected]", g_userID: "user", g_password: "password")
        def math = new PaySvc()
        def another = new PaySvc()
        dept.paySvcs = []
        dept.paySvcs.add(math)
        dept.paySvcs.add(another)
        mockDomain(Department, [dept1, dept2])

        def svcTest = Department.findByName("Dept1")
        assertNotNull svcTest                                           // record Dept1 exists
        assertEquals 2, svcTest.paySvcs.size()                          // 2 paySvcs total

        controller.deleteDept()                                         // calling the action

        svcTest = null
        svcTest = Department.findByName("Dept1")
        assertNotNull svcTest                                           // record Dept1 still exists, it was not deleted
        // can't test render, controller.response.contentAsString is null
        // but testing at the UI is OK
 //       assertEquals "editDept", controller.response.contentAsString    // action should render "editDept"
        assertEquals "editDept", controller.redirectArgs.action

QUESTION:

render(view: "editDept", model: [departmentInstance: departmentInstance]) 

works when testing manually at the UI. But in the integration test the controller.response is null when using render, but returns the expected redirectArg when using redirect.
Any ideas why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我家小可爱 2024-10-09 09:55:15

可能为时已晚,但可能会帮助其他人:
使用render,您需要在控制器上使用modelAndView对象,例如以下示例:

assertEqualscontroller.modelAndView.model.departmentInstance,departmentInstance

assertEquals controller.modelAndView.viewName, "/test/editDept"

祝你好运!

May be far too late but may help someone else:
with render, you need to use the modelAndView object on your controller, such as the following example:

assertEquals controller.modelAndView.model.departmentInstance, departmentInstance

assertEquals controller.modelAndView.viewName, "/test/editDept"

Good luck !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文