[英]How to not display certain errors for Laravel 7 feature test
我的功能測試 LoginTest.php 中有 2 種方法檢查密碼或用戶名是否不正確。
當我運行vendor/bin/phpunit
,我沒有收到任何錯誤報告: OK (40 tests, 154 assertions)
但是,我確實顯示以下錯誤。 這是預期的,因為登錄應該會失敗。
testing.ERROR:提供的授權授權(例如,授權代碼、資源所有者憑據)或刷新令牌無效、已過期、已撤銷、與授權請求中使用的重定向 URI 不匹配,或者已發布給另一個客戶端。 {"exception":"[object] (Laravel\Passport\Exceptions\OAuthServerException(code: 10): 提供的授權授權(例如,授權碼、資源所有者憑據)或刷新令牌無效、過期、撤銷、不匹配授權請求中使用的重定向 URI,或已發布給另一個客戶端。位於 /app/vendor/laravel/passport/src/Http/Controllers/HandlesOAuthErrors.php:26)
這是導致問題的兩種方法。
{
$this->createAccount("test@test.com");
$loginForm = array(
"grant_type" => "password",
"client_id" => "2",
"client_secret" => "lGk9EYlEHdQfXKy0EdTJ5S4Y126y0lkz0ofXiUXe",
"scope" => "*",
"username" => "test@test12.com",
"password" => $this->formData["password"]
);
$response = $this->json('POST', '/oauth/token', $loginForm);
$response->assertStatus(400);
}
public function testLoginPasswordIncorrect()
{
$this->createAccount("test@test.com");
$loginForm = array(
"grant_type" => "password",
"client_id" => "2",
"client_secret" => "lGk9EYlEHdQfXKy0EdTJ5S4Y126y0lkz0ofXiUXe",
"scope" => "*",
"username" => $this->formData["email"],
"password" => "Wrong"
);
$response = $this->json('POST', '/oauth/token', $loginForm);
$response->assertStatus(400);
}
我不應該為這些顯示的錯誤而煩惱嗎? 或者有沒有辦法不顯示它們?
為了避免這些消息,您可以在發出 HTTP 請求之前使用“expectException”和“withoutExceptionHandling”方法。 但是,如果您選擇使用它們,您會得到一個異常而不是 HTTP 響應(因此,您不能使用“assertStatus”方法)。
// ...
$this->withoutExceptionHandling();
$this->expectException(\Laravel\Passport\Exceptions\OAuthServerException::class);
$response = $this->json('POST', '/oauth/token', $loginForm);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.