2010-11-15 235 views
3

控制器動作:集成測試用於控制器

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 

問題:在UI手動測試時

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

作品。但是在集成測試中,當使用渲染時,controller.response爲null,但在使用重定向時返回預期的redirectArg。 任何想法爲什麼?

+1

你有沒有考慮過使用'grails.test.ControllerUnitTestCase'?它節省了大量的設置代碼,並且模擬了渲染和重定向以便於測試。看看http://stackoverflow.com/questions/2846177/unit-testing-a-controller-method/2846306 – ataylor 2010-11-15 21:48:26

+0

其實它是相反的,我試過grails.test.ControllerUnitTestCase。隨着單元測試,你不得不嘲笑更多。 mockForConstraintsTests應該添加域的錯誤集合,但不會。方法findByName()在單元測試中不存在。 – Doris 2010-11-15 22:01:03

+0

儘管有這個名字,你也可以在集成測試中使用'grails.test.ControllerUnitTestCase'基類。 – ataylor 2010-11-15 23:08:00

回答

9

可能爲時已晚,但可以幫助別人: 與render,您需要使用modelAndView對象控制器上,比如下面的例子:

assertEquals controller.modelAndView.model.departmentInstance, departmentInstance

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

祝你好運 !

+0

謝謝這是有幫助的.. – Lucky 2013-06-03 20:06:34

相關問題