2013-07-07 49 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