方法应恰好称为1次,但称为0次

发布于 2025-02-10 03:50:14 字数 659 浏览 1 评论 0原文

测试示例:

$mock = $this->mock(GetAll::class);
$mock->shouldReceive('setCustomersQueryParametersDto')
     ->once()
     ->with($customerDto)
     ->andReturnSelf();

$mock->shouldReceive('execute')->once()->andReturn(collect([])); 

$this->actingAs($user)->json('GET', $this->generateUrl());

它如何工作?我有class getall,该课程在 execute 方法调用之前有一个setter(setCustomerSquerparametersdto),我需要设置我先做的DTO类。然后,我期望在运行测试时,一切都应该很好,但是在我的控制器响应中,执行方法是错误的。

控制器中的代码:

$this->getAllAction->setCustomersQueryParametersDto($customersRequest->model())->execute();

Test example:

$mock = $this->mock(GetAll::class);
$mock->shouldReceive('setCustomersQueryParametersDto')
     ->once()
     ->with($customerDto)
     ->andReturnSelf();

$mock->shouldReceive('execute')->once()->andReturn(collect([])); 

$this->actingAs($user)->json('GET', $this->generateUrl());

How does it work? I have class GetAll and that class has a setter (setCustomersQueryParametersDto) before the execute method call, I need to set my DTO class which I did first. And then I expected when running the test, that everything should be fine, but in my controller response from execute method is wrong.

Code in controller:

$this->getAllAction->setCustomersQueryParametersDto($customersRequest->model())->execute();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2025-02-17 03:50:14

我没有解决我遇到的问题。对我来说,这仍然是一个谜,为什么嘲弄无法执行以前的代码。但是由于我只需要执行方法的结果,所以我发现使用此代码的工作:(

$getAllMock->shouldReceive('setCustomersQueryParametersDto->execute')
           ->andReturn($results);

嘲弄文档中的链方法: http://docs.mockery.io.io/en/latest/reference/reference/demeter_chains.html

)在 getall :: class 中,我只是返回结果,仅此而已。但是在这种情况下,它有效,在一些更复杂的示例中,我可能需要设置一个字段,因此它将很棘手.....

I didn't solve a problem that I had. It is still a mystery to me why Mockery can't execute previous code. But because I only need results from execute method, I find work around with this piece of code:

$getAllMock->shouldReceive('setCustomersQueryParametersDto->execute')
           ->andReturn($results);

(Chain methods in Mockery documentation: http://docs.mockery.io/en/latest/reference/demeter_chains.html)

So I didn't have to bother about fields in GetAll::Class, I simply returned results and that's it. But in this case, it works, in some more complex examples, I will probably need to set a field, so it will be tricky.....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文