2017-08-11 161 views
0

如何瓶坯单元测试这个功能单元测试的Grails控制器

private void secondaryRepError(def secRepCmd) { 

    def repProfile = springSecurityService?.currentUser?.representativeProfile 
    def productTypes = representativeManagementService.getAssignedProductTypes(repProfile) 
    def facilities = representativeManagementService.getAssignedFacilities(repProfile) 
    def facilityType = representativeManagementService.getFacilityType() 
    def listOfRetail = representativeManagementService.listOfFacilities(facilities?.toList(), facilityType?.retail) 
    def listOfLpg  = representativeManagementService.listOfFacilities(facilities?.toList(), facilityType?.lpg) 
    def listOfPipeline = representativeManagementService.listOfFacilities(facilities?.toList(), facilityType?.pipeline) 
    def listOfRelated = representativeManagementService.listOfFacilities(facilities?.toList(), facilityType?.related) 
    render view:'addSecondaryRep', model: [secondaryRepInstance: secRepCmd, appType: ApplicationType.getAll(), productTypes: productTypes, facilities: facilities, retailFacility: facilityType?.retail, lpgFacility: facilityType?.lpg, pipelineFacility: facilityType?.pipeline, facFacility: facilityType?.related, listOfRetail: listOfRetail, listOfLpg: listOfLpg, listOfPipeline: listOfPipeline, listOfRelated:listOfRelated] 


    } 

回答

1

我会建议只是测试所需的所有服务方法调用,利用模拟考试 - 这些调用的不是结果,否则你的测试依赖于那些方法。这些服务方法应该有自己的测试。

而且,测试view是否正确,并且model具有所有必需的属性。

参照,Grails - Unit Testing Controllers

Docs,将帮助您获取当前viewmodel

命令,grails create-unit-test,grails create-unit-test <your-controller-here>,将生成测试类,供您开始使用。