I'm having the same issue. Hope someone more skilled will help to solve this. I would also appreciate to be able to test if the given validation rule was applied on the request.
Apr 25, 2016
4
Level 10
TDD - How to test Controller Validation?
Hi,
We're developing a new system trying to use TDD everywhere, and I'm stuck at Validation tests.
I use the Request classes to validate my API Requests. The problem is how to test it?
I've seen two options:
- Manually test all possible combinations for all fields and make many assertions for it.
- Just check if my Request
rules()method have my expected validations (relying on Laravel Validator) and just check if my Request are being called on my acceptance tests.
I'm tending to use the 2nd approach, but I'm suck on how can I assert that my controller is using the Request class for that call?
My current acceptance test for a update method is:
/** @test */
public function test_update_method()
{
$this->authenticate();
$originalClient = factory(Client::class)->create();
$modifiedClient = factory(Client::class)->make()->toArray();
$this->json('put', '/client/' . $originalClient->id, $modifiedClient);
// ASSERT HERE IF ClientRequest was used on this request
$this->seeStatusCode(200);
$this->seeJson();
$this->checkJsonStructure();
$this->seeInDatabase('clients', $modifiedClient);
}
Please or to participate in this conversation.