在PHPUnit中进行模拟-使用不同的参数对同一方法进行多种配置 [英] Mock in PHPUnit - multiple configuration of the same method with different arguments

查看:48
本文介绍了在PHPUnit中进行模拟-使用不同的参数对同一方法进行多种配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过这种方式配置PHPUnit模拟?

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开始,有

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中进行模拟-使用不同的参数对同一方法进行多种配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆