Skip to content

Commit 37ccd45

Browse files
[11.x] without defer testing docs (#10006)
* without defer * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 1f6303f commit 37ccd45

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

helpers.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,6 +2478,53 @@ protected $middleware = [
24782478
];
24792479
```
24802480

2481+
<a name="disabling-defer-in-tests"></a>
2482+
#### Disabling Deferred Functions in Tests
2483+
2484+
When writing tests, it may be useful to disable deferred functions. You may call `withoutDefer` in your test to instruct Laravel to invoke all deferred functions immediately:
2485+
2486+
```php tab=Pest
2487+
test('without defer', function () {
2488+
$this->withoutDefer();
2489+
2490+
// ...
2491+
});
2492+
```
2493+
2494+
```php tab=PHPUnit
2495+
use Tests\TestCase;
2496+
2497+
class ExampleTest extends TestCase
2498+
{
2499+
public function test_without_defer(): void
2500+
{
2501+
$this->withoutDefer();
2502+
2503+
// ...
2504+
}
2505+
}
2506+
```
2507+
2508+
If you would like to disable deferred functions for all tests within a test case, you may call the `withoutDefer` method from the `setUp` method on your base `TestCase` class:
2509+
2510+
```php
2511+
<?php
2512+
2513+
namespace Tests;
2514+
2515+
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
2516+
2517+
abstract class TestCase extends BaseTestCase
2518+
{
2519+
protected function setUp(): void// [tl! add:start]
2520+
{
2521+
parent::setUp();
2522+
2523+
$this->withoutDefer();
2524+
}// [tl! add:end]
2525+
}
2526+
```
2527+
24812528
<a name="lottery"></a>
24822529
### Lottery
24832530

0 commit comments

Comments
 (0)