Php $this->;的等价物是什么;laravel 8中的withoutExceptionHandling()
所以,我正在使用Laravel8.8编写测试。在以前的其他版本中,在测试时,我可以编写Php $this->;的等价物是什么;laravel 8中的withoutExceptionHandling(),php,laravel,testing,phpunit,Php,Laravel,Testing,Phpunit,所以,我正在使用Laravel8.8编写测试。在以前的其他版本中,在测试时,我可以编写$this->withoutExceptionHandling()来冒泡出确切的错误并知道如何处理它。我对8.8版也有同样的问题 我的密码 <?php namespace Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use T
$this->withoutExceptionHandling()
来冒泡出确切的错误并知道如何处理它。我对8.8版也有同样的问题
我的密码
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\User;
class AuthenticationTest extends TestCase
{
use RefreshDatabase;
/**
* Test if it checks for validation at the point of registarion.
*
* @return void
*/
public function testValidationDuringRegistration()
{
$this->withoutExceptionHandling();
$response = $this->post('/api/v1/register', ['Accept' => 'application/json']);
$response
->assertStatus(406)
->assertJsonValidationErrors(['first_name', 'last_name', 'email', 'password']);
}
/**
* Test if it checks for validation at the point of registarion.
*
* @return void
*/
public function testValidationIfSomeRequiredFieldsAreFilled()
{
$this->withoutExceptionHandling();
$response = $this->post('/api/v1/register', [
'Accept' => 'application/json',
'first_name' => 'test',
'last_name' => 'test_last_name'
]);
$response
->assertStatus(406)
->assertJsonMissingValidationErrors(['first_name', 'last_name']);
}
public function testIfRegistrationIsSuccessful()
{
$this->withoutExceptionHandling();
$response = $this->post('/api/v1/register', [
User::factory()->create()
]);
$response
->assertStatus(200);
}
}
Handler.php
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
//
$this->renderable(function (\AuthenticationException $e, $request) {
// return response()->view('errors.custom', [], 500);
return response()->json([
'success' => false,
'message' => 'Unauthenticated.',
], 403);
});
$this->renderable(function (\ModelNotFoundException $e, $request) {
return response()->json([
'success' => false,
'message' => 'Resource could not be found.',
], 403);
});
$this->renderable(function (\ArgumentCountError $e, $request) {
return response()->json([
'success' => false,
'message' => 'Please provide required url parameters.',
], 403);
});
}
上面的测试一直不及格,但我似乎不知道为什么。我只需要一种方法来输出真正的错误消息,这样我就可以继续前进了
谢谢
我使用的是Laravel 8.8版
FAIL Tests\Feature\AuthenticationTest
⨯ if registration is successful
---
• Tests\Feature\AuthenticationTest > if registration is successful
Expected status code 200 but received 406.
Failed asserting that 200 is identical to 406.
at tests/Feature/AuthenticationTest.php:58
54▕ User::factory()->create()
55▕ ]);
56▕
57▕ $response
➜ 58▕ ->assertStatus(200);
59▕ }
60▕ }
61▕
Tests: 1 failed
Time: 5.66s
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
//
$this->renderable(function (\AuthenticationException $e, $request) {
// return response()->view('errors.custom', [], 500);
return response()->json([
'success' => false,
'message' => 'Unauthenticated.',
], 403);
});
$this->renderable(function (\ModelNotFoundException $e, $request) {
return response()->json([
'success' => false,
'message' => 'Resource could not be found.',
], 403);
});
$this->renderable(function (\ArgumentCountError $e, $request) {
return response()->json([
'success' => false,
'message' => 'Please provide required url parameters.',
], 403);
});
}