gpt4 book ai didi

Laravel Mock with Mockery 有错误 "no expectations were specified"

转载 作者:行者123 更新时间:2023-12-04 11:43:02 26 4
gpt4 key购买 nike

我有这个 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(),但没有指定期望

最佳答案

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

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

关于Laravel Mock with Mockery 有错误 "no expectations were specified",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55117017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com