I prepared a sample API with Laravel. It always returns this object:
{
"a": 1,
"b": 2
}
Now, I'd like to understand better how to test it with phpunit
. I found there are assertJson
and assertJsonFragment
methods and I fail to understand the difference between them.
Let's say I'd like to assert the exact structure of the response, so it needs to be a=1
and b=2
, nothing more, nothing less. At first, I was sure this would work:
$response = $this->get('/api/foo');
$response->assertStatus(200)
->assertJson(['a' => 1, 'b' => 2]);
It will pass the test, but the problem is that it still passes the test if I add more properties to the response, like c=3
or whatever. Then there is assertJsonFragment
method which, for my tests, behaves the same, just giving different error messages.
Is assertJson
buggy? Is there any other way to do what I'm trying to do, which is to just make sure the response is this particular set of properties?
Tested on Laravel 5.6 and 5.7.