Laravel Mock with Mockery has error "no expectations were specified"
Asked Answered
L

1

11

I have this Service class and use repository class in my Service :

class VisitorStatisticAction
{
    protected $repository;

    public function __construct(VisitorStatisticRepository $repository)
    {
        $this->repository = $repository;
    }

    public function setLog($organizationSlug)
    {
        if (!$this->repository->setLog($organizationSlug)) {
            return false;
        }

        return true;
    }
}

I want to write unit test for this class :

public function testSetVisitorLog()
{
    $visitorRepositoryMock = Mockery::mock(VisitorStatisticRepository::class);

    $this->app->instance(VisitorStatisticRepository::class,$visitorRepositoryMock);
    $visitorAction = new VisitorStatisticAction($visitorRepositoryMock);
    $visit = $visitorAction->setLog('gilan');
    $this->assertInstanceOf(VisitorStatistic::class, $visit);
}

but i have this error :

1) Tests\Unit\VisitorStatisticTest::testSetVisitorLog Mockery\Exception\BadMethodCallException: Received Mockery_0_App_Repositories_VisitorStatisticRepository::setLog(), but no expectations were specified

Longdrawnout answered 12/3, 2019 at 8:29 Comment(0)
L
21

I need to use "shouldReceive" and call the method in want to use in :

$visitorRepositoryMock->shouldReceive('setLog')->once()->andReturnSelf();
Longdrawnout answered 12/3, 2019 at 9:5 Comment(1)
Did you put this in addition to $visit = $visitorAction->setLog('gilan'); or replace that line with the above?Navigate

© 2022 - 2024 — McMap. All rights reserved.