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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?