模拟为多个调用指定期望的参数 [英] Mockery specifying expected arguments for multiple calls

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

问题描述

我正在尝试模拟一个对象,该对象获得对同一函数的两次调用,但具有不同的参数.为多次调用返回不同的返回值是很直接的,但是我在任何地方都找不到如何通过参数验证来做到这一点.

I am trying to mock an object that gets two calls to the same function but with different arguments. It's pretty straight forward to give back different return values for multiple calls but I can't find anywhere how to do it with the argument validation.

我尝试过:

$this->eventDispatcher
    ->shouldReceive('dispatch')
    ->twice()
    ->with(Events::SELECT,\Mockery::type('\Not\Really\A\Namespace\Event'))
    ->with(Events::ACTIVITY,\Mockery::type('\Not\Really\A\Namespace\Event');

还有

$this->eventDispatcher
        ->shouldReceive('dispatch')
        ->twice()
        ->with(
            [Events::SELECT,\Mockery::type('\Not\Really\A\Namespace\Event')],
            [Events::ACTIVITY,\Mockery::type('\Not\Really\A\Namespace\Event')]
        );

但是他们不起作用.

从PHPUnit的输出中,我好像得到了一个数组?

From the output PHPUnit gives me it seems like I'm getting an array?

推荐答案

速度很快; P显然,您可以执行此操作,并且效果很好:

Well that was fast ;P Apparently you can do this and it works just fine:

$this->eventDispatcher
    ->shouldReceive('dispatch')
    ->with(Events::SELECT,\Mockery::type('\Not\Really\A\Namespace\Event'));

$this->eventDispatcher
    ->shouldReceive('dispatch')
    ->with(Events::ACTIVITY,\Mockery::type('\Not\Really\A\Namespace\Event'); 

这篇关于模拟为多个调用指定期望的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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