An AuthorizationException is thrown, and then handled by the framework whenever you comment out the line. If you uncomment the line, the exception is thrown but is unhandled, so bubbles all the way up.
Oct 20, 2020
5
Level 10
Test assertStatus(403) fails when $this->withoutExceptionHandling();
Hi!
I get an error, This action is unauthorized. when $this->withoutExceptionHandling(); is in the test. Any ideas why?
TEST:
test('Boss can view users on clinic /users', function() {
$this->withoutExceptionHandling();
actingAs($this->boss)->get('/users')->assertOk();
actingAs($this->user)->get('/users')->assertStatus(403);
actingAs($this->superadmin)->get('/users')->assertOk();
});
If I comment out the $this->withoutExceptionHandling(); the test passes.
ERROR MESSAGE
• Tests\Feature\CreateUserTest > Boss can view users on clinic /users
Illuminate\Auth\Access\AuthorizationException
This action is unauthorized.
at vendor/laravel/framework/src/Illuminate/Auth/Access/Response.php:119
115▕ */
116▕ public function authorize()
117▕ {
118▕ if ($this->denied()) {
➜ 119▕ throw (new AuthorizationException($this->message(), $this->code()))
120▕ ->setResponse($this);
121▕ }
122▕
123▕ return $this;
Thanks in advance!
Level 104
You could expectException(AuthorizationException::class) instead of asserting against the response status code. Or whatever the equivalent is for your test framework (pest? https://pestphp.com/docs/exceptions-and-errors)
Please or to participate in this conversation.