Par*_*ney 4 php phpunit mocking
我有一个单元测试来测试作为数组元素之一的PDOStatement::execute()调用date()。
就像是:
$stmt->execute(array ('value1', 'value2', date('Ymd'));
问题是我的断言$this->anything()用于表示该日期函数结果。我认为它正在破坏,因为它在一个数组中。有没有好的方法来处理这个问题?
我的断言看起来像:
$mock->expects($this->once())
  ->method('execute')
  ->with(array ('value1', 'value2', $this->anything()));
您不能将参数验证方法传递到with()数组内部。PHPUnit 需要迭代数组并检测方法。相反,这些方法之一被传递给with()该方法应该接收的每个参数的方法。
在您的情况下,该方法将接收一个参数,因此您将使用一个验证。您不能使用通用验证,因此您需要使用回调检查数组内部:
$mock->expects($this->once())
     ->method('execute')
     ->with($this->callback(function($array) {
            return 'value1' == $array[0] && 'value2' == $array[1] && 3 == count($array);
        }));
这在PHPUnit 文档中进行了解释。
| 归档时间: | 
 | 
| 查看次数: | 3904 次 | 
| 最近记录: |