Laravel 5.3.8 is now released with new improvements for testing by included new fakes for events, jobs, mail, and notifications.
Here is a quick look at how these new testing featurs work:
Laravel Events
Laravel now provides three helpers for mocking events. The first is expectsEvents
method which verifies the expected events are fired, but prevents any listeners for those events from executing. The second is the inverse doesntExpectEvents
to verify that the given events are not fired:
1public function testOrderShipping()2{3 $this->expectsEvents(OrderShipped::class);4 $this->doesntExpectEvents(OrderFailedToShip::class);
The next helper is withoutEvents
which prevents all events from running:
1public function testUserRegistration()2{3 $this->withoutEvents();
As an alternative to mocking, you may use the Event facade’s fake method to prevent all event listeners from executing.
1use App\Events\OrderShipped; 2use App\Events\OrderFailedToShip; 3use Illuminate\Support\Facades\Event; 4 5class ExampleTest extends TestCase 6{ 7 /** 8 * Test order shipping. 9 */10 public function testOrderShipping()11 {12 Event::fake();1314 // Perform order shipping...1516 Event::assertFired(OrderShipped::class, function ($e) use ($order) {17 return $e->order->id === $order->id;18 });1920 Event::assertNotFired(OrderFailedToShip::class);21 }22}
These same style helpers can also be used in jobs, mail, and notifications and the documentation is already updated with all the details.
Upgrading to 5.3.8 should just be a matter of running composer update
and these new features will be available to be used.
Filed in: