doulongdan2264 2013-06-04 11:11
浏览 25
已采纳

模拟正在测试的当前类的内部函数调用

I want to test this function:

public function reset()
{
    if(!$this->onBeforeReset())      // Protected, internal method
    {
         return false;
    }

    // Continue with tests
}

I'd wish to test it when onBeforeReset function return false or true, too.
How can I do that? Is that possible?
Is there a way to "mock" some parts of the class under test?

  • 写回答

2条回答 默认 最新

  • dongtangxi1584 2013-06-04 11:32
    关注

    Yes, unless the method you want to mock is private or final you can mock just this one method. An example:

    $sut = $this->getMock('Your\Class', array('onBeforeReset'));
    

    The second argument of getMock method is array contains methods that phpunit will mock. If this array is empty phpunit will mock all methods from the object, but if you precise at least one method phpunit will mock only methodz you pass into this array.

    And you mock method as usual:

    $sut->expects($this->any())
        ->method('onBeforeReset')
        ->will($this->returnValue(true));
    

    And finally you can test the method you want to test:

    $this->assertWhatever($expectedValue, $sut->reset());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看