I am running a test to ensure my events are firing properly.
/** @test */
public function foil_products_DO_trigger_oos_event ()
{
$this->setUpTheWorld(true, 1);
$response = $this->sendInStockData($this->pusher, Carbon::now()->subHour());
$this->expectsEvents(\App\Events\InventoryOutOfStock::class);
$this->sendCustomStockData($this->pusher, 0, 1, Carbon::now());
// TEST THE RESULTS OF THE LISTENER FOR THAT EVENT
$this->pusher = $this->pusher->fresh();
$this->assertEquals(1, $this->pusher->oos);
$this->assertCount(2, $this->pusher->inventories, "Pusher doesnt have 2 Inventories");
$this->assertEquals(0, $this->pusher->latestInventory->tags_blocked);
}
If I comment out the line:
$this->expectsEvents(\App\Events\InventoryOutOfStock::class);
Then my test will pass. The line:
$this->assertEquals(1, $this->pusher->oos);
passes and this would only happen if the event fired. Additionally, I looked at my log file and I can guarantee that the event fired....
Is there anything I am doing wrong to tell PHPUnit that these events will be firing???
Thank you!
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
\App\Events\InventoryOutOfStock::class => [
\App\Listeners\InventoryOutOfStockUpdater::class,
\App\Listeners\EmailInventoryOutOfStockNotification::class,
\App\Listeners\SMSInventoryOutOfStockNotification::class,
],
];
/**
* Register any other events for your application.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function boot(DispatcherContract $events)
{
parent::boot($events);
}
}
Kind of an old question but I think previous answer doesn't really capture the issue.
The problem here is that you expect your event to fire and the listener to do something, while THIS WILL NOT HAPPEN here. Calling expectsEvent()
asserts that the event fired but prevents listeners from firing, as official documentation states:
Laravel provides a convenient
expectsEvents
method that verifies the expected events are fired, but prevents any handlers for those events from running.
Read more: https://laravel.com/docs/5.2/testing#mocking-events
Try to simplify and separate your tests. Test either only that an event is fired, or that the listener does the what's expected, depending on complexity and separation of concerns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With