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

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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?