Mock in PHPUnit - multiple configuration of the same method with different arguments(PHPUnit 中的模拟 - 具有不同参数的同一方法的多个配置)
问题描述
可以这样配置PHPUnit mock吗?
Is it possible to configure PHPUnit mock in this way?
$context = $this->getMockBuilder('Context')
->getMock();
$context->expects($this->any())
->method('offsetGet')
->with('Matcher')
->will($this->returnValue(new Matcher()));
$context->expects($this->any())
->method('offsetGet')
->with('Logger')
->will($this->returnValue(new Logger()));
我使用 PHPUnit 3.5.10,当我请求 Matcher 时它失败了,因为它需要Logger"参数.就像第二个期望是重写第一个,但是当我转储模拟时,一切看起来都很好.
I use PHPUnit 3.5.10 and it fails when I ask for Matcher because it expects "Logger" argument. It is like the second expectation is rewriting the first one, but when I dump the mock, everything looks ok.
推荐答案
从 PHPUnit 3.6 开始,有 $this->returnValueMap()
可用于根据给定参数返回不同的值方法存根.
As of PHPUnit 3.6, there is $this->returnValueMap()
which may be used to return different values depending on the given parameters to the method stub.
这篇关于PHPUnit 中的模拟 - 具有不同参数的同一方法的多个配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHPUnit 中的模拟 - 具有不同参数的同一方法的多个配置


基础教程推荐
- php中的PDF导出 2022-01-01
- php中的foreach复选框POST 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- 将变量从树枝传递给 js 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01