测试Facade时无法重新声明:: shouldReceive()

时间:2014-02-01 21:04:29

标签: php unit-testing laravel facade

根据Laravel docs,我应该可以通过Object::shouldReceive()模拟一个外观,但是当我这样做时,我说我无法重新声明该方法。当我在浏览器中查看视图时,视图正常工作,我只需要弄清楚如何正确测试它。

Cannot redeclare Mockery_1_Facades_MyService::shouldReceive() in /path-to-project/vendor/mockery/mockery/library/Mockery/Loader/EvalLoader.php(16) : eval()'d code on line 672

这是我的测试:

public function testSend()
{
    MessageService::shouldReceive('find')->once()->with(1);

    $this->call('GET', '/message/1/send');

    $this->assertRedirectedToRoute('message.index');
}

这是我的控制器:

public function send($id)
{
    $message = MessageService::find($id);
    $this->messageService->sendBroadcast($message);

    return Redirect::route('message.index')
        ->with('message', "Sending message: {$message->name}, stand by...");
}

1 个答案:

答案 0 :(得分:2)

你正在试图嘲笑已经被嘲笑的课程。看看你是否已将服务注册为某个外观 - Laravel有很多自动生成的默认值,这很难被注意到。这是一个外观 shouldReceive 方法:

public static function shouldReceive()
    {
    $name = static::getFacadeAccessor();

    if (static::isMock())
    {
        $mock = static::$resolvedInstance[$name];
    }
    else
    {
        $mock = static::createFreshMockInstance($name);
    }

    return call_user_func_array(array($mock, 'shouldReceive'), func_get_args());
    }