Can't make assertions because exceptions are crashing my test in Laravel
我想在尝试向API发送无效数据时测试响应。
1 2 3 4 5 6 7 8 9 | /** @test */ public function request_schedule_with_invalid_bdate() { $response = $this->json('GET', '/api/schedule', [ 'bdate' => 'thisaintadate', ]); $response->assertStatus(422); } |
根据文档,它应返回422
If the validation rules pass, your code will keep executing normally; however, if validation fails, an exception will be thrown and the proper error response will automatically be sent back to the user. In the case of a traditional HTTP request, a redirect response will be generated, while a JSON response will be sent for AJAX requests.
也,
When using the validate method during an AJAX request, Laravel ... generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code.
听起来Laravel会自动处理抛出的异常并继续发送响应。
但是,在PHPUnit中运行它只会导致错误。
1 2 3 4 | There was 1 error: 1) Tests\Feature\ScheduleTest::request_schedule_with_invalid_bdate Illuminate\Validation\ValidationException: The given data was invalid. |
我读了这个问题但是使用
我的控制器有这个:
1 2 3 4 5 6 7 8 | public function show(Request $request) { $attributes = $request->validate([ 'bdate' => 'required|date' ]); return ['data' =>"It's valid."] } |
这是我的ExceptionHandler类(根据Martin H.的要求)这就是开箱即用的东西。我还没有触及过这门课程。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <?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 equest $request * @param \Exception $exception * @return \Illuminate\Http esponse */ public function render($request, Exception $exception) { return parent::render($request, $exception); } } |
我已确认以下内容:
-
Laravel Framework 5.7.21(
php artisan --version 输出) -
它根据堆栈跟踪运行适当的代码/控制器:
... /供应商/ laravel /框架/ src目录/照亮/确认/ Validator.php:315
... /供应商/ laravel /框架/ SRC /照亮/验证/ Factory.php:136
... /供应商/ laravel /框架/ src目录/照亮/基金/供应商/ FoundationServiceProvider.php:53
... /供应商/ laravel /框架/ src目录/照亮/支持/特征/ Macroable.php:108
... /应用/ HTTP /控制器/ MyController.php:35
我错过了什么?
我遇到了ValidationException崩溃我的测试的问题,因为我禁用了异常处理。 我禁用了异常处理,所以我可以更好地调试我的测试(具有讽刺意味的是,我知道),我忘记了我这样做了。
1 2 3 4 5 6 7 8 9 10 11 | 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(); } |
删除
1 2 3 4 5 | 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. |
相关链接:
- https://github.com/laravel/framework/issues/26013
- laravel phpunit withexceptionhandling