具有多个Expects()调用的PHPUnit模拟 [英] PHPUnit mock with multiple expects() calls

查看:131
本文介绍了具有多个Expects()调用的PHPUnit模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHPUnit,我想知道我们如何从同一个存根/模拟中获得多个期望.

Using PHPUnit, I wonder how we can have multiple expectation from the same stub/mock.

例如,我要测试该模拟将调用方法display()并返回NULL.我还想测试方法process()将被调用.

For example, I want to test that the mock will have the method display() called and return NULL. I also want to test that the method process() will be called.

实际上我的测试称为testProcessIsCalledIfDisplayReturnNull().

所以我需要在同一个模拟对象上设置2个期望值,而手册对此并没有真正帮助:(

So I need to setup 2 expectations on the same mock object, and the manual doesn't really help about that :(

推荐答案

如果您知道,该方法将被调用一次,方法是在Expects()中使用$ this-> once(),否则使用$ this-> any()

If you know, that method is called once use $this->once() in expects(), otherwise use $this->any()

$mock = $this->getMock('nameOfTheClass', array('firstMethod','secondMethod','thirdMethod'));
$mock->expects($this->once())
     ->method('firstMethod')
     ->will($this->returnValue('value'));
$mock->expects($this->once())
     ->method('secondMethod')
     ->will($this->returnValue('value'));
$mock->expects($this->once())
     ->method('thirdMethod')
     ->will($this->returnValue('value'));

这篇关于具有多个Expects()调用的PHPUnit模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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