grails - Integration Test for Controller -
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 ${params.department.name}") } else if (departmentinstance.paysvcs && !departmentinstance.paysvcs.isempty()){ println "instance paysvcs" // !!!! not delete department if 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 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 controller controller.metaclass.message = { map p -> return "foo" } // message dummy returning garbage - work around controller message exception controller.params.department = [name:"dept1", phone:"817-273-3260", g_password:"password", street:"main st.", g_userid:"user", unitcode:"1234567", email:"dept1@yahoo.com", 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: "dept2@yahoo.com", 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: "dept1@yahoo.com", g_userid: "user", g_password: "password") def math = new paysvc() def = 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 action svctest = null svctest = department.findbyname("dept1") assertnotnull svctest // record dept1 still exists, not deleted // can't test render, controller.response.contentasstring null // testing @ ui 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 @ ui. in integration test controller.response null when using render, returns expected redirectarg when using redirect. ideas why?
may far late may else: render
, need use modelandview
object on controller, such following example:
assertequals controller.modelandview.model.departmentinstance, departmentinstance
assertequals controller.modelandview.viewname, "/test/editdept"
good luck !
Comments
Post a Comment