HttpFoundation JsonResponse在PhpUnit测试中返回Null

时间:2020-10-11 17:30:00

标签: json laravel testing phpunit response

我有一个使用Botman的Laravel 8应用程序。当我从Postman呼叫端点时,我得到具有正确标题的正确内容。但是,当我在测试期间调用端点时,我得到的内容为空,因此测试无法通过。响应由HttpFoundation JsonResponse处理。奇怪的是,内容打印在测试控制台中(即使我不转储),但是$ response-> dump()返回Null。

我的测试方法

 /** @test */
public function user_can_talk_to_coach()
{
    Passport::actingAs($this->learner, $this->learnerScopes);

    $response = $this->postJson(
        'learner/v1/coach',
        [
            'driver'  => 'mobile',
            'userId'  => 1,
            'message' => null,
            'intent'  => 'intent_testing',
        ],
        $this->headers
    );

    var_dump('dump');
    $response->dump();
    var_dump('headers');
    $response->dumpHeaders();

    $response->assertStatus(200)
    ->assertJson([
        'data' => [
            [
                'type'                 => 'text',
                'text'                 => 'Hey',
                'attachment'           => null,
                'additionalParameters' => [
                    'node'=> 'testing',
                ],
            ],
        ],
    ]);
}

消息发送方法

/**
 * Send out message response.
 */
public function messagesHandled()
{
    $messages = $this->buildReply($this->replies);

    // Reset replies
    $this->replies = [];

    $response = new JsonResponse();
    $response->setData(['data' => $messages]);
    $response->send();
}

Test result capture

对这里发生的事情有任何想法吗?

0 个答案:

没有答案