2013-07-07 82 views
0

我正在學習如何執行我的一些php代碼的適當單元測試/測試用例。我有一個簡單的功能atMaxLivesAllowed(),我正在測試atMaxLivesAllowedTest()下面。但我不知道如何將值傳遞給getLivesLeftCount()以與max_lives_limit進行比較。我怎樣才能正確地做到這一點?任何想法什麼其他測試可以執行該功能atMaxLivesAllowed()PHPUnit測試 - 單元測試的基本功能

public function getLivesCount(){ 

    $lives = new Life(); 
    $lives->player = $this->id; 
    $lives->lives_left = '1'; 
    $lives->lives_used = '0'; 
    return $player->count(); 

} 

public function atMaxLivesAllowed(){ 

    return $this->getLivesLeftCount() >= $this->max_lives_limit; 
} 

/* 
* Tests the expected behavior of atMaxLivesAllowed 
* 
*/ 
public function atMaxLivesAllowedTest(){ 

    $player->getLivesLeftCount(1); 
    $player->max_lives_limit=5; 

    $this->asertTrue($player->atMaxLivesAllowed()); 
} 

回答

1

假設你的實現是正確的,你得到預期的行爲我沒有看到太多的錯誤與這個測試除了對assert小拼寫錯誤:

它應該是:

$this->assertTrue($player->atMaxLivesAllowed()); 

在另一個說明中,在做TDD時,首先寫測試,它在開始時很棘手,但它非常值得一試,因爲它迫使你考慮程序的不同部分如何交互,是,它迫使你到達關於界面的墨水(公共方法)確實非常好。

,我在你的代碼需要注意的另一件事是,你存儲lives_leftlives_used作爲strings不應該的integers