Grails 2.1 Unit Testing Command Object mockForConstraintsTests not working?

前端 未结 3 436
逝去的感伤
逝去的感伤 2021-01-20 04:45

I have used manually written as well as Grails generated Unit tests for this command object:

   package myapp

    @grails.validation.Validateable
    class          


        
3条回答
  •  长情又很酷
    2021-01-20 04:54

    EDIT

    Using validate() appropriately and mockForConstraintsTest should work if the patch mentioned in the existing Grails bug is in place (Thanks to @codelark for bringing that up). In order to test the command object from a Web App standpoint (using controller) the below information would be helpful.

    Test Command Object Using Controller action:-

    A command object is only deemed as such when it is used as a parameter in one of the action method inside a controller. Refer Command Objects (Warning NOTE). Use SearchCommand in an action method, you should be able to assertEquals.

    Sample:

    void testSomething() {
            YourController controller = mockController(YourController) //Or instantiate
            SearchCommand commandUnderTest = new SearchCommand ()
            //Note the usage here. validate() does not take parameters
            commandUnderTest.basisBuild = ''
            commandUnderTest.validate()
    
            //Call your action
            controller.searchCommandAction(commandUnderTest)
    
            assert response.text == 'Returned'
            assertEquals "blank", commandUnderTest.errors['basisBuild']
        }
    

    YourController's action:-

    def searchCommandAction(SearchCommand sc){
        render "Returned"
    }
    

    Note:

    With out the patch from the grails bug we see the below error in @Grails 2.1.4, 2.2.0 & 2.2.1

    I get an error when I only correct the validation and use mockForConstraintTests without using controller action:

    enter image description here

提交回复
热议问题