简体   繁体   English

我如何在laravel中编写函数返回json数据的测试

[英]how i can write test for function return json data in laravel

I have a function it return json data 我有一个函数它返回json数据

return response()->json([
  'status' => 'OK',
  'data' => [
      'id' => $dataInfo->id,
  ],
]);

how I can solve this in testing for function ? 我如何在功能测试中解决这个问题?

You could have a look at: https://laravel.com/docs/5.4/http-tests#testing-json-apis 您可以查看: https//laravel.com/docs/5.4/http-tests#testing-json-apis

Example (add this in your unit test file): 示例(在单元测试文件中添加):

$this->json('post', 'your/route')
     ->assertStatus(200)
     ->assertJson([
        'status' => 'OK',
     ]);

You can use assertJson() and other similar methods in Laravel 5.4: 你可以在Laravel 5.4中使用assertJson()和其他类似的方法:

 $this->json('put', 'api/some-url')
      ->assertStatus(200)
      ->assertJson([
          'status' => 'OK',
          'data' => [
              'id' => 5,
          ]
      ]);

https://laravel.com/docs/5.4/http-tests#testing-json-apis https://laravel.com/docs/5.4/http-tests#testing-json-apis

In Laravel 5.3 and lower, JSON testing methods are different. 在Laravel 5.3及更低版本中,JSON测试方法是不同的。 Mostly used one is seeJson() . 最常用的是seeJson()

https://laravel.com/docs/5.3/application-testing#testing-json-apis https://laravel.com/docs/5.3/application-testing#testing-json-apis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM