dongyun65343 2016-05-25 11:58
浏览 35

PHPunit:在模拟查询类型方法上调用预期总是不好的做法吗?

I'm unit testing my php code using PhpUnit. Many developers say that is bad practice to put expectation on query method, that is method that only returns something and does not change state of system ( CQS ). Expectations should be used only on command type methods. For Example:

$this->validatorMock->expects($this->once())
               ->method('isValid')
               ->with('foo')
               ->willReturn(true)

Is considered a bad practice because it couple test to tightly to implementation. Correct way:

$this->validatorMock->method('isValid')
     ->willReturn(true);

However what if I'm testing class that depends on results of validation of some sort eg.:

if ($this->validator->isValid($data) {
         // some logic
} else {
     // some other logic
}                                    

Should I put expectation on that method or not? It's a query type method but it is important to test that it was called. I want to know that data was validated.

Of course isValid method itself is tested in seperate testClass ( one that tests validator class )

  • 写回答

1条回答 默认 最新

  • dongyuan6949 2016-05-25 12:36
    关注

    There are lots of functions that don't modify a state, but just return a value based on input. Validation is one type, conversion is another type.

    It makes perfect sense to unit test those functions, because they contain important logic that you don't want to break. I think 'many developers' are wrong here.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?