Swaz's avatar
Level 20

See what validation rules are failing in a test?

Sometimes I want to test a feature that happens after a form is submitted. In these cases, I often don't care about any of the data being posted.

The code below is one of the ways I've gone about doing this. The problem is, sometimes my factories get out of date and no longer produce code that will pass my form validation. When the validation is checking 20+ fields it can be challenging to figure out what rule is actually failing.

public function test_something()
{
    $product = Product::factory()->create();

    $this->post('/products', $product->toArray());

    // Assert stuff here, but validation is failing...
}

Is there any way to see exactly what validation rule is failing?

I've tried accessing errors off the session but that doesn't seem to work.

public function test_something()
{
    $product = Product::factory()->create();

    $response = $this->post('/products', $product->toArray());

    // Doesn't work
    dd($response->session()->get('errors'));
}
0 likes
3 replies
Tray2's avatar

Create stores it in the database you should use make instead.

I recommend using a dataprovider it makes it must cleaner

   /**
     * @test
     * @dataProvider storeValidationProvider
     * @param $fieldValue
     * @param $field
     */
    public function store_validation_tests($field, $fieldValue)
    {
        $book = Book::factory()->make([
            $field => $fieldValue
        ]);

        $response = $this->post('/books', $book->toArray());
        $response->assertStatus(302);
        $response->assertSessionHasErrorsIn($field);
    }

    public function storeValidationProvider()
    {
        return [
            'the title is required' => ['title', ''],
            'part must be numeric' => ['part', 'One'],
            'format_id is required' => ['format_id', ''],
            'format_id must exist in formats' => ['format_id', 100],
            'genre_id is required' => ['genre_id', ''],
            'genre_id must exist in genres' => ['genre_id', 100],
            'isbn is required' => ['isbn', ''],
            'invalid isbn10 cant be stored' => ['isbn', '123456789'],
            'invalid isbn13 cant be stored' => ['isbn', '9771234567890'],
            'released is required' => ['released', ''],
            'pages is required' => ['pages', ''],
            'pages must be numeric' => ['pages', 'Ten'],
            'blurb is required' => ['blurb', ''],
        ];
    }
Swaz's avatar
Level 20

Thanks for the reply!

I use make() when I can, but sometimes I need it saved to the database.

Regardless, I'm not trying to test validation here. I'm trying to test that an action happens after a form is submitted, but one of my validation rules is failing. I'm just looking for a quick way to see what the problem is without manually checking each validation rule against the provided value.

Swaz's avatar
Swaz
OP
Best Answer
Level 20

I figured it out.

public function test_something()
{
    $product = Product::factory()->create();

    $response = $this->post('/products', $product->toArray());

    // Works
    dd(session('errors'));
}

Please or to participate in this conversation.