2013-11-27 123 views
0

我有一個關於PHPUnit的查詢。如何在PHPUnit中爲不在基類本身中處理的情況創建測試用例。例如,請考慮以下showMessage方法。PHPUnit - 測試不是由基本函數處理的案例

Class Message 
{ 

    public function showMessage($type = 1) 
    { 

     if(1 == $type){ 
      return 'Success'; 
     }elseif(2 == $type){ 
      return 'ERROR'; 
     } 

    } 

} 

在上述showMessage方法,也應該有,如果$type是其他任何比1或2,所以它應該有一個別的情況下的情況下,這樣的條件應該是這樣的

if(1 == $type){ 
     return 'Success'; 
    }elseif(2 == $type){ 
     return 'ERROR'; 
    }else{ 
     return 'UNKNOWN'; 
    } 

感謝您的任何輸入

回答

1

不能確定你是問什麼,但我給它一個鏡頭..

要測試showMessage方法在你的第一個代碼塊中,你會這樣做,因爲函數將返回null,當指定return或者只有return;是。

public function testShowMessage() 
{ 
    $msg = new Message(); 

    $this->assertEquals('Success', $msg->showMessage(1)); 
    $this->assertEquals('ERROR', $msg->showMessage(2)); 
    $this->assertEquals(null, $msg->showMessage(-1)); 
    $this->assertEquals(null, $msg->showMessage(3)); 
    $this->assertEquals(null, $msg->showMessage('test')); 
} 

測試用例你的第二個方法是:

public function testShowMessage() 
{ 
    $msg = new Message(); 

    $this->assertEquals('Success', $msg->showMessage(1)); 
    $this->assertEquals('ERROR', $msg->showMessage(2)); 
    $this->assertEquals('UNKNOWN', $msg->showMessage(-1)); 
    $this->assertEquals('UNKNOWN', $msg->showMessage(3)); 
    $this->assertEquals('UNKNOWN', $msg->showMessage('test')); 
} 

showMessage方法在我看來是寫的方法應該拋出一個異常,如果它沒有通過01因爲它強化了更好的設計,而不僅僅是將任何舊值傳遞給方法。我會重構這個方法的東西像這樣:

/** 
* @param int $type 
* @return string 
* @throws InvalidArgumentException 
*/ 
public function showMessage($type) 
{ 
    // Will be cast to 0 if it cannot be converted. 0 is not used in this method. 
    switch ((int) $type) { 
     case 1: 
      return 'Success'; 
     case 2: 
      return 'ERROR'; 
     default: 
      throw new InvalidArgumentException("\$type requires an integer value of 1 or 2."); 
    } 

} 

雖然我不會設計這種方法的擺在首位,這是製造最好的這種情況。以下是相關的測試用例:

public function testShowMessage() 
{ 
    $msg = new Message(); 

    $this->assertEquals('Success', $msg->showMessage(1)); 
    $this->assertEquals('ERROR', $msg->showMessage(2)); 
} 

/** 
* @expectedException InvalidArgumentException 
*/ 
public function testShowMessageExceptionWithInteger() 
{ 
    $msg = new Message(); 
    $msg->showMessage(0); 
} 

/** 
* @expectedException InvalidArgumentException 
*/ 
public function testShowMessageExceptionWithString() 
{ 
    $msg = new Message(); 
    $msg->showMessage("test"); 
} 

在你原來的方法指定$type作爲可選的參數,它似乎要添加的所有這種迴避代碼從任何方法/數據是通話/傳送的距離自己遠到showMessage()方法。另一個例子是返回UNKNOWN。在此之前應該有檢查或異常情況,以便在測試時可以很容易地發現。這可能會在以後引入錯誤。

希望能夠解決問題。

+1

我唯一要補充的是使用一個常數而不是1/2,這樣在調用代碼中就可以解釋你所調用的內容,並且這些數字可以在將來改變而不需要重構。 showMessage(消息:: SUCCESS);這將定義常量。潛在地,Message :: SUCCESS可能是其他的東西,比如提示消息框(showMessage(Message :: OK_BUTTON_ONLY)); –

+0

@StevenScott我同意,我在寫它的時候考慮過了,但我也不想偏離它很多 – SamV

+0

@FruityP感謝您的回覆,如果我的問題很混亂,我很抱歉,讓我告訴你爲什麼我問了這個問題,我需要爲我的同伴編寫的代碼編寫測試用例,並且我注意到他沒有在他的代碼中覆蓋'else {return'UNKNOWN';}'部分,所以我想知道如何編寫一個測試用例,通過在他的showMessage()方法中傳遞一個值來使代碼失敗在他的代碼中沒有被他處理,我希望我現在很清楚:)謝謝 – user2909892