How can I write a test to return a json function in laravel

I have a function that returns json data

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

      

how can i solve this when testing the function?

+3


source to share


2 answers


You can take a look: https://laravel.com/docs/5.4/http-tests#testing-json-apis

Example (add this to your unit test file):



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

      

+1


source


You can use assertJson()

other similar methods in Laravel 5.4:

 $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



In Laravel 5.3 and below, JSON testing methods are different. Mostly used seeJson()

.

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

+1


source







All Articles