How to Mock Command Object that is inside Controller

前端 未结 1 1678
余生分开走
余生分开走 2021-01-07 00:17

I have a controller class, inside of which i have a command object. I have a method find() which uses this command object as follows:

class itemController{

         


        
相关标签:
1条回答
  • 2021-01-07 00:41

    You can use mockCommandObject

    Class RioController

    class RioController {
        class UserCommand{
            String email
            static constraints = {
                email blank: false, email: true
            }
        }
    
        def load={UserCommand cmd -> 
            if(cmd.validate()){
                flash.message = "Ok"
            }
            else{
                flash.message = "Where is the email?"
            }
        }
    }
    

    Class RioControllerTests

    import grails.test.mixin.*
    import org.junit.*
    
    @TestFor(RioController)
    class RioControllerTests {
    
        @Test
        void testLoad(){
            mockCommandObject RioController.UserCommand
            controller.load()
            assert flash.message == "Where is the email?"
    
            params.email = "verynew@email.com"
            controller.load()
            assert flash.message == "Ok"
        }
    }
    
    0 讨论(0)
提交回复
热议问题