Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot redeclare ::shouldReceive() when testing Facade

According to the Laravel docs, I should be able to mock a facade via Object::shouldReceive(), but when I do it says I cannot redeclare the method. The view works correctly when I view it in my browser, I just need to figure out how to test this correctly.

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

Here's my test:

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

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

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

Here's my controller:

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...");
}
like image 309
Webnet Avatar asked Oct 21 '22 15:10

Webnet


1 Answers

You are trying to mock already mocked class. See if you have registered your service somewhere as a facade - Laravel has a lot of auto generated defaults, which is hard to notice. Here is a facades shouldReceive method:

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());
    }
like image 91
Miroslav Trninic Avatar answered Oct 27 '22 10:10

Miroslav Trninic