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 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题
  • ¥15 使用dify通过OpenAI 的API keys添加OpenAI模型时报了“Connection Error”错误
  • ¥40 想找个软件,但我不知道怎么找到它,我甚至不知道有没有
  • ¥15 怎么把60秒的视频时长改成显示0秒?且视频内容没有任何变化的正常播放?目的是为了解决一些平台对视频时长的要求,最好有自动处理的工具!
  • ¥15 累加器设初值为00H