3

I'm in the process of writing a web app using Laravel 5.5 and Vue.js. PHPUnit version is 6.3.1.

I'm testing for validation errors when a user registers using Form Requests.

Route:

// web.php
Route::post('/register', 'Auth\RegisterController@store')->name('register.store');

This is my passing test:

/** @test */
function validation_fails_if_username_is_missing()
{
    $this->withExceptionHandling();

    $this->json('POST', route('register.store'), [
        'email' => 'johndoe@example.com',
        'password' => 'secret',
        'password_confirmation' => 'secret'
    ])->assertStatus(422);
}

However, it fails when I remove exception handling:

/** @test */
function validation_fails_if_username_is_missing()
{
    $this->json('POST', route('register.store'), [
        'email' => 'johndoe@example.com',
        'password' => 'secret',
        'password_confirmation' => 'secret'
    ])->assertStatus(422);
}

I do not understand why this test fails without exception handling as it's stated in the Laravel documentation that

If the request was an AJAX request, a HTTP response with a 422 status code will be returned

I already tried to declare this particular route in the api middleware group, but that didn't change anything.

Can someone with more experience than I do explain to me why that is? Thanks in advance.

EDIT: This is the content of my Handler.php class file. I don't think anything was edited.

protected $dontReport = [
    \Illuminate\Auth\AuthenticationException::class,
    \Illuminate\Auth\Access\AuthorizationException::class,
    \Symfony\Component\HttpKernel\Exception\HttpException::class,
    \Illuminate\Database\Eloquent\ModelNotFoundException::class,
    \Illuminate\Session\TokenMismatchException::class,
    \Illuminate\Validation\ValidationException::class,
];


public function report(Exception $exception)
{
    parent::report($exception);
}

public function render($request, Exception $exception)
{
    return parent::render($request, $exception);
}

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest(route('login'));
}
Pixlforge
  • 31
  • 6
  • Do you have any custom code in `Handler.php`? – Marcin Nabiałek Sep 28 '17 at 15:38
  • No I don't think I've added or edited anything in this file. I've added the content of the file to the original post. – Pixlforge Sep 28 '17 at 15:45
  • I have the same issue, interesting thing is that according to this guide: https://laracasts.com/series/whats-new-in-laravel-5-5/episodes/17. We shouldn't get exception by default, only if we turn it on using withExceptionHandling. However our cases are quite opposite – Sabine Aug 03 '18 at 07:59

0 Answers0