簡體   English   中英

PHPUnit測試-單元測試的基本功能

[英]PHPUnit testing- Unit test a basic function

我正在學習如何對我的某些php代碼執行適當的單元測試/測試用例。 我有一個簡單的函數atMaxLivesAllowed() ,正在下面測試atMaxLivesAllowedTest() 但是我不確定如何將值傳遞給getLivesLeftCount()以與max_lives_limit進行比較。 如何正確執行此操作? 有什么想法我可以對atMaxLivesAllowed()函數執行什么其他測試?

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());
}

假設您的實現是正確的,並且您得到了預期的行為,那么我對這個測試沒有發現太多的錯誤,除了對assert一些錯誤拼寫:

它應該是:

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

另一個要注意的是,在進行TDD時,您首先要編寫測試,雖然一開始很棘手,但值得一搏,因為它迫使您考慮程序的不同部分將如何交互,也就是說,它迫使您考慮一下確實很好的接口(公共方法)。

我在您的代碼中注意到的另一件事是,您將lives_leftlives_used用作strings存儲,它們不應該是integers嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM