【问题标题】:phpunit mock expectation failed for method name is equal方法名称相等的phpunit模拟期望失败
【发布时间】:2013-12-09 09:45:31
【问题描述】:

我有以下科目要测试:

class ReportTable_Renderer_Html_Decorator_AddRecord extends ReportTable_Renderer_Html_Decorator_CallParent
{
    public function renderAddItem(ReportTable $table)
    {
        $newRow = array();

        $masterIDColumn = $this->getMasterIDColumn();
        if (!empty($masterIDColumn)) {
            $newRow[$masterIDColumn] = $this->getOwner()->getMasterID();
        }

        foreach ($table->getColumns() as $name => $column) {
            $newRow[$name] = '';
        }
        $newRow['id']               = '0';
        if (!empty($newRow[$masterIDColumn])) $newRow['id'] .= '_' . $newRow[$masterIDColumn];
        $newRow[$this->getColumn()] = $this->getText();

        $this->getRowStyle()->getGroupStyles()->add('do_not_print grey');

        return $this->getParent()->renderRowContent($table, $newRow);
    }

还有这个(间接)父类,我需要为测试存根它的函数

class ReportTable_Renderer_Html_Decorator_Base extends ReportTable_Renderer_Html
{
    public function renderRowContent(ReportTable $table, array $row) {}
    public function renderRowSetFooter(ReportTable $table) {}
}

我的测试:

public function testRenderRowSetFooter()
{
    $table = new ReportTable('a','b');
    $table->addColumn( new ReportTable_Column( 'one', 'one' ));
    $table->addColumn( new ReportTable_Column( 'two', 'two' ));
    $table->addColumn( new ReportTable_Column( 'three', 'three' ));

    $testText = 'test text';
    $parentFooterText = 'parent.parent';
    $groupID = 234;
    $addText = 'Add me. Add me now!';
    $newRow = array('one' => $addText, 'two' => $groupID, 'three' => '', 'id' => 0 );
    $parent = $this->getMock('ReportTable_Renderer_Html_Base', array( 'renderRowContent', 'renderRowSetFooter' ));
    $parent->expects($this->any())->method('renderRowContent')->with($table, $newRow)->will($this->returnValue($testText));
    $parent->expects($this->any())->method('renderRowSetFooter')->with($table)->will($this->returnValue($parentFooterText));
    $subject = $this->getSubject($parent, array( 'text' => $addText, 'column' => 'one', 'masterIDColumn' => 'two' ));
    $subject->getOwner()->setMasterID($groupID);
    $this->assertEquals($parentFooterText . $testText, $subject->renderRowSetFooter($table));
}

我对这两个模拟函数都会出现的错误消息感到困惑:

PHPUnit_Framework_ExpectationFailedException : Expectation failed for method name is     equal to <string:renderRowContent> when invoked zero or more times
Parameter 1 for invocation     Herkt_ReportTable_Renderer_Html_Base::renderRowContent(Herkt_ReportTable Object (...), Array     (...)) does not match expected value.
Failed asserting that two arrays are equal.

其中一个数组显示的是 $newRow,另一个显然是函数的结果。但是我没有为这些数组添加 assertEquals 吗?这是怎么发生的?如何修复我的测试?

【问题讨论】:

    标签: unit-testing parameters mocking phpunit


    【解决方案1】:

    好的,想通了。我继承了这个测试并正在调整它以适应变化的功能。发生的情况是,由于模拟函数,实际测试通过将 $newRow 传递给模拟函数 renderRowContent 来进行

    我的测试失败,因为我没有根据测试函数的变化调整预期的参数

    应该是:

    $newRow = array(
                'masterColumn' => $groupID, 
                'one' => $addText, 
                'two' => '', 
                'three' => '', 
                'id' => '0_234'
              );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-24
      • 2020-09-01
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 2021-12-21
      相关资源
      最近更新 更多