3

I have a function it return json data

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

how I can solve this in testing for function ?

2 Answers 2

1

You could have a look at: 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',
     ]);
Sign up to request clarification or add additional context in comments.

1 Comment

PHPUnit_Framework_Exception: Argument #1 (No Value) of PHPUnit_Framework_Assert::assertJson() must be a string I got this
1

You can use assertJson() and 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 lower, JSON testing methods are different. Mostly used one is seeJson().

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

2 Comments

@flower usually you're expecting some specified data while testing response.
i try it :) but i got this error PHPUnit_Framework_Exception: Argument #1 (No Value) of PHPUnit_Framework_Assert::assertJson() must be a string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.