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 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)