Php mockry-call_user_func_array()要求参数1为有效回调

Php mockry-call_user_func_array()要求参数1为有效回调,php,mocking,phpunit,silex,Php,Mocking,Phpunit,Silex,我有一门课需要模拟: 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

我有一门课需要模拟:

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);
    }
}
我使用的是mockry0.7.2

$mediaPublisherMock = \Mockery::mock('MessagePublisher')
    ->shouldReceive('publish')
    ->withAnyArgs()
    ->times(3)
    ->andReturn(null);
不幸的是,由于这个错误,我的测试失败了

call\u user\u func\u array()要求参数1为有效回调, 类“mockry\expection”中没有方法“publish” /供应商/mockry/mockry/library/mockry/compositeexpection.php 在线54

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

public function __call($method, array $args)
{
    foreach ($this->_expectations as $expectation) {
        call_user_func_array(array($expectation, $method), $args);
    }
    return $this;
}
其中
$method='publish'
$args=array()
$expectation是mockry\expectation对象()的实例


我使用的是PHP5.3.10,你知道怎么了吗

我相信$expectation应该是您的类,MessagePublisher

使用标准PhpUnit模拟库可以解决问题

这项工作:

$mediaPublisherMock = $this->getMock('Mrok\Model\MessagePublisher', array('publish'), array(), '', false);
$mediaPublisherMock->expects($this->once())
    ->method('publish');

为什么我没有从这里开始;)

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

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

我检查了两次-它是mockry\Expectation,它包含属性_mock(mockry\mock)和_name=字符串“publish”,或者将代码分成两行:$mediaPublisherMock=\mockry::mock('MessagePublisher')$mediaPublisherMock>shouldReceive('publish')->withAnyArgs()->times(3)->andReturn(null);这就是我的解决方案。