Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Adds assertJsonPath to TestResponse #29957

Merged
merged 3 commits into from
Sep 13, 2019
Merged

[6.x] Adds assertJsonPath to TestResponse #29957

merged 3 commits into from
Sep 13, 2019

Conversation

ecrmnn
Copy link
Contributor

@ecrmnn ecrmnn commented Sep 11, 2019

In our test we want to assert the following:

  • The post has no tags
  • The first comment belongs to the user ecrmnn
  • The body of the first comment is First! and the body of the second comment is This is my comment

Given the following response from our Post API:

{
  "title": "My blog post",
  "body": "Lorem ipsum ...",
  "tags": [],
  "comments": [{
  	"body": "First!",
  	"user_id": 42,
  	"user": {
  		"id": 42,
  		"username": "ecrmnn"
  	}
  }, {
  	"body": "This is my comment",
  	"user_id": 731,
  	"user": {
  		"id": 731,
  		"username": "ventrec"
  	}
  }]
}

The problem

Using assertEquals

- Forces us to create a temporary variable
- Assertions are not chainable

$response = $this->getJson(route('api.post.show', [$post->id]));

$this->assertEmpty($response->json('tags'));
$this->assertEquals('ecrmnn', $response->json('comments.0.user.username'));
$this->assertEquals([
    'First!',
    'This is my comment',
], $response->json('comments.*.body'));

Using assertExactJson

- Can not ignore irrelevant data
- Forces us to provide the entire response to make three simple assertions

$this->getJson(route('api.post.show', [$post->id]))
	->assertExactJson([
	    'title' => 'My blog post',
	    'body' => 'Lorem ipsum ...',
	    'tags' => [],
	    'comments' => [
	        [
	            'body' => 'First!',
	            'user_id' => 42,
	            'user' => [
	                'id' => 42,
	                'username' => 'ecrmnn',
	            ],
	        ],
	        [
	            'body' => 'This is my comment',
	            'user_id' => 731,
	            'user' => [
	                'id' => 731,
	                'username' => 'ventrec',
	            ],
	        ],
	    ],
	]);

Using assertJsonFragment

- We don't know if the first or the second user is the one named ecrmnn
- Unable to assert on the body of the first and second comment

$this->getJson(route('api.post.show', [$post->id]))
	->assertJsonFragment([
	    'tags' => [],
	    'username' => 'ecrmnn',
	]);

The solution

This PR introduces assertJsonPath in the TestResponse class. It makes it possible to assert that a given value exists at the specified path.

Using assertJsonPath

+ No need for a temporary variable
+ Lets us assert on a specific user
+ No need to include irrelevant data in the assertion

$this->getJson(route('api.post.show', [$post->id]))
    ->assertJsonPath('tags', [])
    ->assertJsonPath('comments.0.user.username', 'ecrmnn')
    ->assertJsonPath('comments.*.body', [
        'First!',
        'This is my comment',
    ]);

@GrahamCampbell GrahamCampbell changed the title Adds assertJsonPath to TestResponse [6x] Adds assertJsonPath to TestResponse Sep 11, 2019
@GrahamCampbell GrahamCampbell changed the title [6x] Adds assertJsonPath to TestResponse [6.x] Adds assertJsonPath to TestResponse Sep 11, 2019
@taylorotwell taylorotwell merged commit 5726387 into laravel:6.x Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants