【问题标题】:phpunit mock multiple methods in a single callphpunit 在一次调用中模拟多个方法
【发布时间】:2020-02-17 01:45:42
【问题描述】:

有两个模拟方法 setAsy1setAsy2 返回相同的值。目前我需要调用两次相同的方法函数来设置模拟方法。有什么方法可以通过一个电话进行设置吗?

$transferItemMockf->expects($this->any())
    ->method('setAsy1')
    ->willReturn($id);

$transferItemMockf->expects($this->any())
    ->method('setAsy2')
    ->willReturn($id);

【问题讨论】:

    标签: php mocking phpunit refactoring


    【解决方案1】:

    PHPUnit\Framework\MockObject\Builder\InvocationMocker::method() 可以得到PHPUnit\Framework\Constraint\Constraint 作为参数。因此,您可以通过一次调用从您的示例中设置模拟:

    $transferItemMockf->expects($this->any())
       ->method($this->logicalOr(
            $this->equalTo('setAsy1'),
            $this->equalTo('setAsy2')
        ))
       ->willReturn($id);
    

    【讨论】:

      猜你喜欢
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 2020-11-15
      • 2012-06-12
      • 2010-12-16
      • 2010-09-25
      • 2019-07-29
      • 1970-01-01
      相关资源
      最近更新 更多