phpunit模拟期望失败,方法名称相等

时间:2013-11-24 19:39:35

标签: unit-testing parameters mocking phpunit

我有以下主题要测试:

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?这是怎么产生的,如何修复我的测试?

1 个答案:

答案 0 :(得分:2)

好的,想通了。我继承了这个测试并且正在调整它以改变功能。会发生什么是因为模拟函数,实际测试是通过将$ newRow传递给mock函数renderRowContent

来实现的。

所以我的测试失败了,因为我没有使预期参数适应我测试函数的变化

它需要

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