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 中的模拟 - 具有不同参数的同一方法的多个配置
基础教程推荐
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
