Skip to content

Conversation

@Gimcrack
Copy link
Contributor

@Gimcrack Gimcrack commented Nov 1, 2017

This commit adds two assert helpers to the TestResponse class.

assertValidationErrors( string|array $keys )

Asserts that the given key(s) are in the errors key of the Json response.

Sample use case - validating a new user

// UserController@store
$request->validate([
    'first_name' => 'required',
    'last_name' => 'required'
]);



// Feature/UserTest
$this->post('users', [
    'first_name' => null,
    'last_name' => null
]);

$this
  ->response()
  ->assertValidationErrors(['first_name','last_name']);


// sample response
array:2 [
  "message" => "The given data was invalid."
  "errors" => array:2 [
    "first_name" => array:1 [
      0 => "The first_name field is required."
    ],
    "last_name" => array:1 [
      0 => "The last_name field is required."
    ]
  ]
]

// the test passes

assertJsonCount( int $count )

Asserts that the response data has the expected count.

Sample use case - getting a user resource index

// Feature/UserTest
/** @test */
public function it_can_get_a_listing_of_users()
{
    $users = factory(User::class,3)->create();

    $this->get("users");

    $this->response()
        ->assertStatus(200)
        ->assertJsonCount(3);
}

@thecrypticace
Copy link
Contributor

I really like assertJsonCount. It's a good option for when you need to assert that you received only N records in an array. assertJson deals in fragments so it is unable to cover this case and assertExactJson can end up being too verbose if you're asserting on targeted data.

However, this does not work as expected if all your responses have a top-level data key (or something similar) How about adding an optional key (w/ a default value of null) It would allow assertions on data like so:

$response->assertJsonCount(3, "data")
$response->assertJsonCount(9, "data.*.contacts")

You should be able to assertCount on the result of data_get($this->json(), $key) as, iirc, data_get will return the original array if you pass null

Copy link
Contributor

@carusogabriel carusogabriel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix this PHP Docblocks and you're ready to go 😄


/**
* Assert that the response json has the expected count.
* @param int $count
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be:

/**
 * Assert that the response json has the expected count.
 *
 * @param int $count
 * @return $this
 */

/**
* Assert that the response has validation errors for the given keys.
*
* @param string|array $keys
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be:

/**
 * Assert that the response has validation errors for the given keys.
 *
 * @param string|array $keys
 * @return $this
 */

@Dylan-DPC-zz
Copy link

I think it is better to have a provision for custom validation messages and localisation

@taylorotwell
Copy link
Member

I agree that assertJsonCount isn't very helpful without being able to provide a key.

@Gimcrack
Copy link
Contributor Author

Gimcrack commented Nov 3, 2017

@taylorotwell Good catch. Now if you are using pagination or similar you can specify a key.

$this->response()->assertJsonCount(3, 'data');

@carusogabriel
Copy link
Contributor

Rebase your commits to a single one 😊

If you don’t know how: http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html

@Dylan-DPC-zz
Copy link

@Gabriel-Caruso there is an option to squash while merging the PR. So Taylor can take care of that

@carusogabriel
Copy link
Contributor

@Dylan-DPC Oh, I didn’t know about this option 😱😍

@taylorotwell taylorotwell merged commit 15b3eef into laravel:5.5 Nov 6, 2017
@mvdnbrk
Copy link
Contributor

mvdnbrk commented Nov 9, 2017

In the release notes this is mentioned as added assertValidationErrors. Taylor changed this to assertJsonValidationErrors. 😄

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.

7 participants