Why validate redirects instead of returning the json response?

52 Views Asked by At

I created this test in loginTest, it's almost the same as code_is_required in another test (createdUserTest).

They use the same assertion that I created for myself (assertJsonApiValidationErrors):

/**
 @test
 */
public function email_is_required()
{
    $this->withoutExceptionHandling();

    // Send a request without password
    Role::factory(3)->create();
    User::factory(1)->create([
        'state' => '1'
    ])->first();

    $response = $this->post(
        route('api.user.login'),
        [
            'data' => [
                'type' => 'user',
                'attributes' => [
                    'password' => 'password'
                ]
            ]
        ]
    );

    // Using the macro create in MakesJsonRequest to validate
    $response->assertJsonApiValidationErrors('email');
}

My controller login:

function login(Request $request)
{
    $request->validate([
        'data.attributes.email' => 'required',
        'data.attributes.password' => 'required',
    ]);
}

If I don't put the validate, the test says "Don't find error for the field", but if I put the validate, that doesn't work:

  FAILED  Tests\Feature\SessionTest > email is required                         ValidationException   
  The data.attributes.email field is required.

  Tests:    2 failed, 3 passed (18 assertions)
  Duration: 0.81s

The macro validates a specific structure the same as in the other test (createUserTest) and I overwrite the exception to pass. But in the login, the test doesn't work

macro

handle

0

There are 0 best solutions below