【问题标题】:call parent function of mocked class调用模拟类的父函数
【发布时间】:2014-02-18 16:24:08
【问题描述】:

我有以下问题。

class A
{
    public function isNew()
    {
        return ($this->ID == 0);
    }
}

    class B extends A  
    {
      //Some functions  
    }

现在我想模拟B类。所以我有一些陈述

$oMockedStm = $this->getMockBuilder('B')->getMock();        
$oMockedStm->expects($this->any())->method('someMethod')->will($this->returnValue(TRUE));           
$oMockedStm->expects($this->any())->method('anotherMethod')->will($this->returnValue(TRUE));

现在当我这样做时

$this->assertTrue($oMockedStm->isNew());

我收到错误:断言 null 为真失败。

这怎么可能。该函数总是返回真假。

这与不能调用mocked对象的父方法有关吗?

【问题讨论】:

    标签: unit-testing mocking phpunit


    【解决方案1】:

    我发现我不想嘲笑整个班级。只有特定的功能。 因此,在定义模拟对象时,您需要使用 setMethods() 函数来指定要模拟的特定函数。

    像这样:

    $oMockedStm = $this->getMockBuilder('B')
                    ->setMethods(array('someMethod','anotherMethod'))
                    ->getMock();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      相关资源
      最近更新 更多