dongliechuich10319 2014-02-21 16:11
浏览 137
已采纳

具有依赖注入的同一类的Mock方法 - PHP

i was wondering on how i can mock a method on the same class that this class have a dependency itself.

Let's say an example:

Class SomeClassA {

  protected $dependency;

  function __construct(Dependency $dependency) {

     $this->dependency = $dependency;

  }


  public function methodToTest() {

         if ( !is_null( $this->methodCalled() ) )

            return $this->methodCalled();

         else

            return "it\'s not ok!"

         // need to test this method mocking the methodCalled()

  }


  public function methodCalled($message) {

        return $message;

  }


}

if this class did not have the dependency i could do something like so:

public function test_methodToTest() {

    $this->getMock('SomeClassA',['methodCalled'])
         ->with('message')
         ->expects($this->once())
         ->will($this->returnValue('message'));

   $someClassA = new SomeClassA();

   $this->assertEquals('message',$someClassA->methodToTest('message'));


}

Edit:

Let's say this example to improve the meaning of my question i just changed a little bit the previous example:

Class SomeClassA {

  protected $dependency;

  function __construct(Dependency $dependency) {

     $this->dependency = $dependency;

  }


  public function methodToTest() {

         if ( $this->methodCalled() ) // i was wondering how mock this method

            return $this->dependency->someDependencyMethod(); // not this

         else

            return "it\'s not ok!"



  }


  public function methodCalled() {

        return true;

  }


}

I just didn't understand how I can mock the method $this->methodClass(). I found hard it because the class SomeClassA have a dependency. And i'm testing a method of that class.

  • 写回答

2条回答 默认 最新

  • dozabg1616 2014-02-24 07:10
    关注

    You can do as follows:

    class SomeClassATest {
        public function testMethodToTest() {
            $someClassA = $this->getMockBuilder('SomeClassA')
                ->setMethods(array('methodCalled'))
                ->getMock();
            $someClassA->expects($this->once())
                ->method('methodCalled')
                ->will($this->returnValue('your mocked value here'));
            $actual = $someClassA->methodToTest();
            $this->assertEquals('your expectation here', $actual);
        }
    }
    

    This will only work if methodCalled is either public or protected, since you can only mock public and protected methods. The way it works up there is you are testing $someClassA, a mock instance of SomeClassA, where only methodCalled is replaced with your test behavior, and everything else stays the same.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败