2013-11-24 84 views
0

我有以下的受測試: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,另一個顯然是該函數的resut。但是我沒有爲這些數組添加一個assertEquals?這是怎麼產生的?我該如何解決我的測試問題?

回答

2

好的,算出來了。我繼承了這個測試,並將其改編爲更改後的功能。什麼情況是,由於模擬功能,實際檢測情況通過傳遞$ NEWROW進入模擬功能renderRowContent

所以我的測試失敗,因爲我沒有在我的測試功能,適應預期的參數變化

它需要是

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