0

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?

3
  • Try to add $this->withExceptionHandling(); in your test method. Commented May 6, 2022 at 19:05
  • @xuma that would be the solution, but if the author needs to do so, then something is wrong, as that is the default behavior and should never be changed, because this will happen Commented May 6, 2022 at 22:46
  • @xuma, I tried your suggestion but unfortunately id didn't work Commented May 9, 2022 at 8:47

1 Answer 1

0

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.