简体   繁体   中英

Unit testing Grails controller

how to preform unit test this function

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]


  }

I would recommend to just test all the required service method invocations, using mocks -- not the results of those invocation, otherwise your test depends on those method. Those service methods should have their own tests.

Moreover, test whether the view is correct or not, and model has all the required properties.

Refer, Grails - Unit Testing Controllers .

Docs , will help you in getting current view and model .

Command, grails create-unit-test , grails create-unit-test <your-controller-here> , will generate the test class for you to start with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM