'Laravel: Feature test json validations unique rule issue

I recently started implementing the unit testing in Laravel 9.x framework. So far, I was able to write some basic rules without any complications. However, in my application I am validating the forms using ajax and From Request for validation rules.

CategoryRequest.php

class CategoryRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            // ...

            'title' => [
                'required',
                'max:255',
                function($attributes, $value, $fails) {
                    $Category = Category::where([
                        'user_id' => request()->user_id,
                        'title'   => request()->title,
                    ])->first();

                    if($Category) {
                        $fails('`Category` is already taken');
                    }
                }
            ],

        // ...
        ];
    }
}

CategoryTest.php

class CategoryTest extends TestCase
{
    use RefreshDatabase;

    // ...

    public function test_new_category_with_unique_validation()
    {
        $user = $this->__user();
        $arrayPost = [
            'user_id' => $user->id,
            'uuid'    => Str::uuid(),
            'title'   => 'title',
            'status'  => STATUS['active'],
        ];

        Category::factory()->create($arrayPost);

        $response = $this
            ->actingAs($user)
            ->post('/console/categories', $arrayPost);

        $response->assertJsonValidationErrorFor('title');
        $response->assertJsonValidationErrorFor('status');
        $response->assertJsonValidationErrorFor('uuid');
        $response->assertJsonValidationErrorFor('user_id');
    }

    private function __user(): object
    {
        return User::factory()->create();
    }
}

I am getting the following error...

error

What am I doing wrong?



Solution 1:[1]

I found the solution here assert Json Validation Errors does not work

Also, I have created the separate tests for each validation. Therefore, this test only focuses on the unique title validation now.

class CategoryTest extends TestCase
{
    use RefreshDatabase;

    // ...

    public function test_new_category_with_unique_title_validation()
    {
        $user = $this->__user();

        Category::factory()->create([
            'user_id' => $user->id,
            'uuid'    => Str::uuid(),
            'title'   => 'title',
            'status'  => STATUS['active'],
        ]);

        $response = $this->actingAs($user)->json('POST', '/console/categories', [
            'title'  => 'title',
            'status' => STATUS['active'],
        ], [
            'Accept' => 'application/json'
        ]);

        $response->assertJsonValidationErrorFor('title');
    }

    private function __user(): object
    {
        return User::factory()->create();
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MrSingh