【问题标题】:How to mock public methods inside controller如何在控制器内模拟公共方法
【发布时间】:2015-01-08 06:40:01
【问题描述】:

我有一个控制器,它有很多动作。在动作内部,它正在调用驻留在同一类中的其他方法。我如何模拟这些方法以使其返回我想要的。

SomeController{

    def action1={

       method1() //i want to mock this method. this is not a service method

       }

    public def method1(){
        //some code here
       }


    def action2={

       method2() // i want to mock this method
     }

    public def method2(){

       //some code here
     }



}// SomeController

我想模拟 method1() 和 method2() 这样它应该返回我定义的东西。 提前感谢您的帮助!

【问题讨论】:

    标签: unit-testing grails junit


    【解决方案1】:

    回答自己

     controller.metaClass.method1(){
    
      //define whatever require
      // some codes
    
     }
    
     controller.metaClass.method2(){
    
     // define whatever require
     // some code
    
     }
    

    以上代码运行良好

    感谢您的宝贵时间!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 2013-05-15
      相关资源
      最近更新 更多