3 years ago

TDD - How to test Controller Validation?

Posted 3 years ago by EliasSoares


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:

  1. Manually test all possible combinations for all fields and make many assertions for it.
  2. 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()

        $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->seeInDatabase('clients', $modifiedClient);

Please sign in or create an account to participate in this conversation.