麻烦伪造laravel httpclient响应

发布于 2025-01-31 04:00:50 字数 1009 浏览 3 评论 0原文

我正在尝试测试以下代码:

”在此处输入图像说明“

DimonAclient只是围绕Laravel HTTPClient的简单包装器;这里的简化函数在这里:

“在此处输入图像描述”

getDeclaration()响应是\ Illuminate \ http \ client \ wister \ worlds \ worlds


我在测试中要做的是:

  1. 模拟DimonAclient类,我不创建一个实际的API调用
  2. “模拟”(使用Laravel的HTTP :: Response())我想要的响应,以便我可以测试200 w/某些状态会派遣适当的事件(也嘲笑,但在这里不相关)

我的测试代码看起来像这样:

  1. ​会满足(新响应需要消息接口等,等等...)
  2. 我实际上不需要getDeclaration()返回测试的任何内容,所以我想知道在任何情况下我是否应该以不同的方式模拟这一点(我是基础 内部代码上的这个假设

在http ::响应上处理我正在测试的 圆圈试图正确地将其连接起来。

蒂亚!

I am trying to test the following bit of code:

enter image description here

DimonaClient is just a simple wrapper around a Laravel HttpClient; simplified function here:

enter image description here

The getDeclaration() response is a \Illuminate\Http\Client\Response


What I am trying to do in my test is:

  1. Mock the DimonaClient class so I don't create an actual api call
  2. "Mock" (use Laravel's Http::response()) the response I want so that I can test that a 200 w/ certain statuses dispatches the appropriate event (also mocked, but not relevant here)

My test code looks like this:

enter image description here

My issue(s) seem to be:

  1. the getDeclaration() has an expectation of Illuminate\Http\Client\Response but I can't seem to create anything that will satisfy that (a new Response wants a MessageInterface, etc, etc... )
  2. I don't actually need getDeclaration() to return anything for my testing, so I wonder if I should be mocking this differently in any case (I base this assumption on Http::response handling the internal code I'm testing for things like $response->ok(), instead of a Mockery expectation)

I feel like I'm one small step away from making this work, but going round in circles trying to hook it up correctly.

TIA!

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

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

发布评论

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

评论(1

疾风者 2025-02-07 04:00:50

如果您使用的是http立面,则无需模拟dimonAcient。您几乎在测试中就在那里,但是让我向您展示您将要做的事情:

/** @test */
public function it_can_handle_an_approved_submission(): void
{
    Http::fake([
        '*' => Http::response([
            'declarationStatus' => [
                'result' => DimonaDeclarationStatus::ACCEPTED,
                'dimonaPeriodId' => $this->faker->numerify('############'),
            ],
        ],
    ]);

    $dimonaDeclarationId = $this->faker->numerify('############');

    // Do your normal call, and then assertions
}

这样做,您会告诉http伪造任何URL,因为我们使用* 。我建议您使用$ this-> endpoint/$ neclarationId,因此,如果不匹配,您也会知道您没有达到正确的端点。


我不确定您正在使用什么laravel,但这可以使用,因为Laravel 6+,请检查 http伪造的URL

If you are using Http Facade, you don't need to mock DimonaCient. You are nearly there with your test, but let me show you what you would have done:

/** @test */
public function it_can_handle_an_approved_submission(): void
{
    Http::fake([
        '*' => Http::response([
            'declarationStatus' => [
                'result' => DimonaDeclarationStatus::ACCEPTED,
                'dimonaPeriodId' => $this->faker->numerify('############'),
            ],
        ],
    ]);

    $dimonaDeclarationId = $this->faker->numerify('############');

    // Do your normal call, and then assertions
}

Doing this, you will tell Http to fake any URL, because we are using *. I would recommend you use $this->endpoint/$declarationId so if it does not match, you will also know you did not hit the right endpoint.


I am not sure what Laravel you are using but this is available since Laravel 6+, check Http fake URLs.

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