Laravel Mockery与Mockery发生错误“未指定期望”

时间:2019-03-12 08:29:10

标签: laravel unit-testing mockery

我有这个Service类,并在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;
    }
}

我想为此课程编写单元测试:

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);
}

但是我有这个错误:

1)Tests \ Unit \ VisitorStatisticTest :: testSetVisitorLog Mockery \ Exception \ BadMethodCallException:收到Mockery_0_App_Repositories_VisitorStatisticRepository :: setLog(),但未指定期望

1 个答案:

答案 0 :(得分:0)

我需要使用“ shouldReceive”并在要使用的方法中调用方法:

$visitorRepositoryMock->shouldReceive('setLog')->once()->andReturnSelf();