With Exceptions::fake()
, you can simulate exception handling in tests and use methods like assertReported
to confirm that specific exceptions were thrown, without actually stopping test execution.
use App\Exceptions\UnauthorizedException;
use Illuminate\Support\Facades\Exceptions;
test('unauthorized exception is thrown', function () {
Exceptions::fake();
$response = $this->get('/admin/dashboard');
// Assert that UnauthorizedException was reported
Exceptions::assertReported(UnauthorizedException::class);
// Assert specific details of the exception
Exceptions::assertReported(function (UnauthorizedException $e) {
return $e->getMessage() === 'You do not have access to this section.';
});
});