douxun4924
2013-07-07 18:55
浏览 7
已采纳

PHPUnit测试 - 单元测试一个基本功能

I am learning how to perform proper unit testing/test cases of some of my php code. I have a simple function atMaxLivesAllowed() that I am testing atMaxLivesAllowedTest() below. But I am not sure how to pass a value to getLivesLeftCount() to compare to max_lives_limit. How can I do this correctly? any ideas what other test can I perform for that function 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());
}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpca4790 2013-07-07 21:36
    已采纳

    Assuming that your implementation is correct and you're getting the expected behaviour I don't see that much wrong with this test except for on little misspelling of assert:

    It should be:

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

    On another note, when doing TDD, you write the tests first, It is tricky in the beginning but it is well worth the struggle because it forces you to think about how the different parts of your program will interact, that is, it forces you to think about the interfaces (public methods) which is very good indeed.

    Another thing that I notice in your code is that you store the lives_left and lives_used as strings shouldn't they be integers?

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题