方法应恰好称为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有解决我遇到的问题。对我来说,这仍然是一个谜,为什么嘲弄无法执行以前的代码。但是由于我只需要执行方法的结果,所以我发现使用此代码的工作:(
嘲弄文档中的链方法: 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:
(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.....