Skip to content

Commit 6a842f4

Browse files
committed
wip
1 parent e1735c4 commit 6a842f4

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

http-tests.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,69 @@ class ExampleTest extends TestCase
347347
<a name="exception-handling"></a>
348348
### Exception Handling
349349

350-
Sometimes you may want to test that your application is throwing a specific exception. To ensure that the exception does not get caught by Laravel's exception handler and returned as an HTTP response, you may invoke the `withoutExceptionHandling` method before making your request:
350+
Sometimes you may need to test that your application is throwing a specific exception. To accomplish this, you may "fake" the exception handler via the `Exceptions` facade. Once the exception handler has been faked, you may utilize the `assertReported` and `assertNotReported` methods to make assertions against exceptions that were thrown during the request:
351+
352+
```php tab=Pest
353+
<?php
354+
355+
use App\Exceptions\InvalidOrderException;
356+
use Illuminate\Support\Facades\Exceptions;
357+
358+
test('exception is thrown', function () {
359+
Exceptions::fake();
360+
361+
$response = $this->get('/order/1');
362+
363+
// Assert an exception was thrown...
364+
Exceptions::assertReported(InvalidOrderException::class);
365+
366+
// Assert against the exception...
367+
Exceptions::assertReported(function (InvalidOrderException $e) {
368+
return $e->getMessage() === 'The order was invalid.';
369+
});
370+
});
371+
```
372+
373+
```php tab=PHPUnit
374+
<?php
375+
376+
namespace Tests\Feature;
377+
378+
use App\Exceptions\InvalidOrderException;
379+
use Illuminate\Support\Facades\Exceptions;
380+
use Tests\TestCase;
381+
382+
class ExampleTest extends TestCase
383+
{
384+
/**
385+
* A basic test example.
386+
*/
387+
public function test_exception_is_thrown(): void
388+
{
389+
Exceptions::fake();
390+
391+
$response = $this->get('/');
392+
393+
// Assert an exception was thrown...
394+
Exceptions::assertReported(InvalidOrderException::class);
395+
396+
// Assert against the exception...
397+
Exceptions::assertReported(function (InvalidOrderException $e) {
398+
return $e->getMessage() === 'The order was invalid.';
399+
});
400+
}
401+
}
402+
```
403+
404+
The `assertNotReported` and `assertNothingReported` methods may be used to assert that a given exception was not thrown during the request or that no exceptions were thrown:
405+
406+
```php
407+
Exceptions::assertNotReported(InvalidOrderException::class);
408+
409+
Exceptions::assertNothingReported();
410+
```
411+
412+
You may totally disable exception handling for a given request by invoking the `withoutExceptionHandling` method before making your request:
351413

352414
$response = $this->withoutExceptionHandling()->get('/');
353415

@@ -816,6 +878,11 @@ You may use the `component` method to evaluate and render a [Blade component](/d
816878

817879
$view->assertSee('Taylor');
818880

881+
<a name="testing-exceptions"></a>
882+
## Testing Exceptions
883+
884+
If you are testing a
885+
819886
<a name="available-assertions"></a>
820887
## Available Assertions
821888

0 commit comments

Comments
 (0)