本文介绍了模拟-call_user_func_array()期望参数1为有效的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要模拟的课程:

I have a class I need to mock:

class MessagePublisher
{
    /**
     * @param \PhpAmqpLib\Message\AMQPMessage $msg
     * @param string $exchange - if not provided then one passed in constructor is used
     * @param string $routing_key
     * @param bool $mandatory
     * @param bool $immediate
     * @param null $ticket
     */
    public function publish(AMQPMessage $msg, $exchange = "", $routing_key = "", $mandatory = false, $immediate = false, $ticket = null)
    {
        if (empty($exchange)) {
            $exchange = $this->exchangeName;
        }

        $this->channel->basic_publish($msg, $exchange, $routing_key, $mandatory, $immediate, $ticket);
    }
}

我正在使用Mockery 0.7.2

I am using Mockery 0.7.2

$mediaPublisherMock = \Mockery::mock('MessagePublisher')
    ->shouldReceive('publish')
    ->withAnyArgs()
    ->times(3)
    ->andReturn(null);

不幸的是,由于此错误,我的测试失败了

unfortunately my tests failed, due to this error

我尝试调试,发现此代码中的测试失败

I have tried to debug I found that tests fails in this code

public function __call($method, array $args)
{
    foreach ($this->_expectations as $expectation) {
        call_user_func_array(array($expectation, $method), $args);
    }
    return $this;
}

其中
$ method ='发布'
$ args = array()
$ expectation是Mockery \ Expectation对象()的实例

where
$method = 'publish'
$args = array()
$expectation is instance of Mockery\Expectation object ()

我正在使用php 5.3.10-知道什么地方错了吗?

I am using php 5.3.10 - any idea what is wrong?

推荐答案

之所以发生这种情况,是因为您将模拟期望分配给了$mediaPublisherMock,而不是模拟本身.尝试将getMock方法添加到该调用的末尾,例如:

This is happening because you are assigning a mock expectation to $mediaPublisherMock, rather than the mock itself. Try adding the getMock method to the end of that call, like:

$mediaPublisherMock = \Mockery::mock('MessagePublisher')
    ->shouldReceive('publish')
    ->withAnyArgs()
    ->times(3)
    ->andReturn(null)
    ->getMock();

这篇关于模拟-call_user_func_array()期望参数1为有效的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 02:40