2016-09-22 24 views
1

我測試簡單工廠類與它返回一個TagModel單個方法構造的對象參數的順序。如何測試的傳遞給通過一種方法

class TagFactory 
{ 
    public function buildFromArray(array $tagData) 
    { 
     return new TagModel(
      $tagData['t_id'], 
      $tagData['t_promotion_id'], 
      $tagData['t_type_id'], 
      $tagData['t_value'] 
     ); 
    } 
} 

我可以測試方法...

public function testbuildFromArray() 
{ 
    $tagData = [ 
     't_id' => 1, 
     't_promotion_id' => 2, 
     't_type_id' => 3, 
     't_value' => 'You are valued', 
    ];  

    $tagFactory = new TagFactory(); 
    $result = $tagFactory->buildFromArray($tagData); 
    $this->assertInstanceOf(TagModel::class, $result); 
} 

如果我更改了new TagModel…參數的順序測試仍然會通過。

如果我prophesize的TagModel ...

$tagModel = $this->prophesize(TagModel::class); 
    $tagModel->willBeConstructedWith(
     [ 
      $tagData['t_id'], 
      $tagData['t_promotion_id'], 
      $tagData['t_type_id'], 
      $tagData['t_value'] 
     ] 
    ); 

...但我應該怎麼那麼可以斷言? assertSame不起作用,因爲它們不是。

我可以測試從TagModel的干將順序,但那時我已經超越了測試只是這個單位。但我確實認爲應該測試訂單,因爲如果我改變它們,測試仍然通過。

回答

1

你正在測試的方法是一個工廠。它創建一個對象。如果確保它是預期的類型對您來說不夠用,則需要驗證其狀態。可以用getter檢查它,也可以創建一個你期望接收的對象,並使用assertEquals()來比較它。

+0

謝謝,我已經做了,這意味着這個測試基本上成爲TagModel測試以及測試的參數是正確的順序通過。 – deadlyhifi

相關問題