dsa5211314 2015-12-07 14:25
浏览 43
已采纳

PHPUnit只测试一些参数

For example, if there is the method
public function validate($param1, $param2) { // validate $param1 // validate $param2 }

Can I just test $param1 to be validated??

I mean something like mocking the method so when validating $param2, it will always return TRUE.. So it just validate the $param1..
Is it able to do that?

  • 写回答

2条回答 默认 最新

  • douliao8318 2015-12-08 02:14
    关注

    Don't mock your own code that is about to be tested.

    If you have written a method that needs two parameters and will validate them, you have to provide two values for every test.

    If this is annoying, because the two parameters don't have anything in common besides being validated in the same method, this is a true sign for badly designed code. If it is annoying to test something, then it probably is annoying to use that code as well.

    Take these hints as tasks to improve the code. If you have two absolutely independent values that can be validated separately, then do separate them. Have a validation class for each one of them, test the validation thoroughly there. Then have another class that accepts these two validators, and test that with one call of the method, the first parameter is passed to the first validator (mocked!), and the second one to the second validator (also mocked). Done!

    You end up with one class resembling something like a form with multiple fields, and two individual validators that can be easily reused.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀