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条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型