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()
controller.metaClass.message = { Map p -> return "foo" }
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 another = new PaySvc()
dept.paySvcs = []
dept.paySvcs.add(math)
dept.paySvcs.add(another)
mockDomain(Department, [dept1, dept2])
def svcTest = Department.findByName("Dept1")
assertNotNull svcTest
assertEquals 2, svcTest.paySvcs.size()
controller.deleteDept()
svcTest = null
svcTest = Department.findByName("Dept1")
assertNotNull svcTest
assertEquals "editDept", controller.redirectArgs.action
Question:
render(view: "editDept", model: [departmentInstance: departmentInstance])
works when testing manually in the user interface. But in the integration test, controller.response is null when using render, but returns redirectArg when using redirection. Any ideas why?