Can';我不能断言,因为异常正在破坏我在Laravel中的测试

Can';我不能断言,因为异常正在破坏我在Laravel中的测试,laravel,phpunit,Laravel,Phpunit,我想在尝试向API发送无效数据时测试响应 /** @test */ public function request_schedule_with_invalid_bdate() { $response = $this->json('GET', '/api/schedule', [ 'bdate' => 'thisaintadate', ]); $response->assertStatus(422); } 根据报告,它应该返回422 如果验

我想在尝试向API发送无效数据时测试响应

/** @test */
public function request_schedule_with_invalid_bdate()
{
    $response = $this->json('GET', '/api/schedule', [
      'bdate' => 'thisaintadate',
    ]);

    $response->assertStatus(422);
}
根据报告,它应该返回422

如果验证规则通过,代码将继续正常执行;但是,如果验证失败,将抛出异常,并自动将正确的错误响应发送回用户。对于传统的HTTP请求,将生成重定向响应,而对于AJAX请求将发送JSON响应

而且

在AJAX请求期间使用validate方法时,Laravel。。。生成包含所有验证错误的JSON响应。此JSON响应将与422 HTTP状态代码一起发送

听起来Laravel应该自动处理抛出的异常并继续发送响应

There was 1 failure:

1) Tests\Feature\ScheduleTest::request_schedule_with_invalid_bdate
Expected status code 200 but received 422.
Failed asserting that false is true.
但是,在PHPUnit中运行此命令只会导致错误

There was 1 error:

1) Tests\Feature\ScheduleTest::request_schedule_with_invalid_bdate
Illuminate\Validation\ValidationException: The given data was invalid.
我阅读,但使用
$this->expectException(…)将使测试通过,但不运行我的断言。如果我断言状态不是422,它仍然会通过

我的控制器有以下功能:

public function show(Request $request) 
{
  $attributes = $request->validate([
    'bdate' => 'required|date'
  ]);

  return ['data' => "It's valid."]
}
这是我的ExceptionHandler类(根据Martin H的要求),这是现成的。我还没碰过这门课

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }
}


我在ValidationException导致测试崩溃时遇到问题,因为我禁用了异常处理。我禁用了异常处理,以便更好地调试测试(我知道这很讽刺),但我忘记了我是这么做的

class ... extends TestCase 
{
    protected function setUp()
    {
        /**
         * This disables the exception handling to display the stacktrace on the console
         * the same way as it shown on the browser
         */
        parent::setUp();
        $this->withoutExceptionHandling();
    }
删除
$this->withoutExceptionHandling()现在允许我对响应进行断言

There was 1 failure:

1) Tests\Feature\ScheduleTest::request_schedule_with_invalid_bdate
Expected status code 200 but received 422.
Failed asserting that false is true.
相关链接: -
-

我强烈建议您发布异常处理程序类,其中有fault@MartinHenriksen我添加了ExceptionHandler类。我找到了测试崩溃的原因。