0

我不知道这是不是有意的。但是当我在控制器中使用响应代码 204 时,我在 PHPunit 中得到“从路由返回无效 JSON”。

但是,如果我将响应代码更改为 201,一切正常……为什么?

protected function output($title, $status = 201)
{
    return response([
        'title' => $title,
        'offers' => $this->popular,
    ], $status);
}

$this->output('Empty', 204); //Here is the error

在我的测试中

/** @test */
public function popular_can_fetch_food_lists() {
    $result = $this->json('post', route('popular', [
        'type' => $this->food->type,
        'id' => $this->food->id
    ]))->json();
}
4

1 回答 1

1

如果您查看https://httpstatuses.com/204,您可以阅读:

204 响应由标头字段后的第一个空行终止,因为它不能包含消息正文。

因此,如果您实际上将 204 用于任何事情,则不会有任何内容响应。

所以你不应该->json()在你的测试中运行。您应该只验证状态码是否为 204

于 2018-06-16T18:16:04.837 回答