【问题标题】:Laravel testing, get JSON contentLaravel 测试,获取 JSON 内容
【发布时间】:2015-11-19 10:12:16
【问题描述】:

在 Laravel 的单元测试中,我可以像这样测试 JSON API:

$this->post('/user', ['name' => 'Sally'])
    ->seeJson([
        'created' => true,
    ]);

但是如果我想使用响应呢?如何使用$this->post() 获取 JSON 响应(作为数组)?

【问题讨论】:

  • $this->getResponse()->getContent() 可能会成功。

标签: laravel laravel-5 phpunit


【解决方案1】:

获取内容的正确方法是:

$content = $this->get('/v1/users/1')->decodeResponseJson();

【讨论】:

  • 我同意。适用于 5.4。
  • 也适用于 5.7。
  • 6.3.3:抛出 decodeResponseJson() 已被弃用的异常。会检查文档,但我们都知道那是多么毫无意义......
  • 除了 Docs 之外,您可以使用的方法的完整列表在 Illuminate\Foundation\Testing\TestResponse 类(Laravel 6 lts)中。
  • 也适用于 8.40
【解决方案2】:

目前在 5.3 中,这正在工作...

$content = $this->get('/v1/users/1')->response->getContent();

它确实打破了链条,但是因为response 返回的是响应而不是测试运行器。因此,您应该在获取响应之前进行可链接的断言,就像这样......

$content = $this->get('/v1/users/1')->seeStatusCode(200)->response->getContent();

【讨论】:

  • 在 Laravel 7 中使用 decodeResponseJson 是正确的方法。这将返回字符串。
  • 我如何查看response 可以使用哪些方法?
【解决方案3】:

我喜欢在处理 json 时使用 json 方法,而不是 ->get()

$data = $this->json('GET', $url)->seeStatusCode(200)->decodeResponseJson();

【讨论】:

    【解决方案4】:

    在 Laravel 8 中,这对我有用。 在 POST 请求创建实体后,我返回了一个自动生成的字段(余额)。 响应在结构{"attributes":{"balance":12345}}

    $response = $this->postJson('api/v1/authors', [
        'firstName' => 'John',
        'lastName' => 'Doe',
    ])->assertStatus(201);
    
    $balance = $response->decodeResponseJson()['attributes']['balance'];
    

    decodeResponseJson 将选择响应并将其转换为数组以进行操作。 使用 getContent() 会返回 json,您必须在返回的数据上使用 json_decode 才能将其转换为数组。

    【讨论】:

    • 适用于版本 8.+
    【解决方案5】:

    我遇到了类似的问题,无法使用内置的 $this->get() 方法使 $this->getResponse()->getContent() 工作。我尝试了几种变体都没有成功。

    相反,我不得不更改调用以返回完整的 http 响应并从中获取内容。

    // Original (not working)
    $content = $this->get('/v1/users/1')->getContent();
    
    // New (working)
    $content = $this->call('GET', '/v1/users/1')->getContent();
    

    【讨论】:

    • 测试用例没有getContent() 方法。 get() 方法是一个流利的帮助器,它返回测试用例(因此您可以链接多个断言)。测试用例确实有一个response 属性,它将返回您的响应对象(而不是测试用例)。然后您可以在该响应对象上调用getContent()。详情请参阅我的回答。
    【解决方案6】:

    简单的方法:

    $this->getJson('api/threads')->content()
    

    【讨论】:

    • 这里使用 json() 代替 content() 也可以对响应数据进行 json 解码
    • 您也可以使用 getOriginalContent(),因为它以数组形式返回。
    【解决方案7】:

    找到更好的方法:

    $response = $this->json('POST', '/order', $data);
    $responseData = $response->getOriginalContent(); // saves the response as an array
    $responseData['value']  // you can now access the array values
    
    

    此方法将响应 json 作为数组返回。

    【讨论】:

      【解决方案8】:

      只是想分享一下,我在$this->json()也用过like:

      $response = $this->json('POST', '/order', $data)->response->getContent();
      

      但我又添加了一行来使用 json 响应和解码,否则 decodeResponseJson() 对我不起作用。

      $json = json_decode($response);
      

      【讨论】:

      • 可能是版本问题
      猜你喜欢
      • 1970-01-01
      • 2021-08-04
      • 1970-01-01
      • 2018-05-08
      • 2018-05-27
      • 2016-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多