laravel phpunit(带例外处理)
我正在使用Laravel5.5和Vue.js编写一个web应用程序。PHPUnit版本是6.3.1 当用户使用表单请求注册时,我正在测试验证错误 路线:laravel phpunit(带例外处理),phpunit,response,laravel-5.5,http-status-code-422,Phpunit,Response,Laravel 5.5,Http Status Code 422,我正在使用Laravel5.5和Vue.js编写一个web应用程序。PHPUnit版本是6.3.1 当用户使用表单请求注册时,我正在测试验证错误 路线: // web.php Route::post('/register', 'Auth\RegisterController@store')->name('register.store'); 这是我的及格考试: /** @test */ function validation_fails_if_username_is_missing() {
// web.php
Route::post('/register', 'Auth\RegisterController@store')->name('register.store');
这是我的及格考试:
/** @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);
}
但是,当我删除异常处理时,它会失败:
/** @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);
}
我不理解为什么这个测试在没有异常处理的情况下失败,正如Laravel文档中所述
如果请求是AJAX请求,则为状态为422的HTTP响应
代码将被返回
我已经尝试在api中间件组中声明这个特定的路由,但这并没有改变任何事情
有没有比我更有经验的人能解释一下原因?提前谢谢
编辑:这是我的Handler.php类文件的内容。我认为没有任何东西被编辑过
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'));
}
你在
Handler.php
中有自定义代码吗?没有,我想我没有在这个文件中添加或编辑任何东西。我已经将该文件的内容添加到原始帖子中。我也有同样的问题,有趣的是根据以下指南:。默认情况下,我们不应该获取异常,除非我们使用withExceptionHandling启用它。然而,我们的情况恰恰相反