class ExampleTest extends TestCase
{
public function test_event_is_triggered()
{
Event::fake();
$this->get('your/api/route');
// Assert that an event was dispatched...
Event::assertDispatched(MyEvent::class);
}
}
How to test an event that is triggered by a request done with something different then $this->get()
I'm trying to find the best way to test if an event is triggered when a specific endpoint of my API is requested. Normally I'm testing events like this:
Event::fake([
MyEvent::class,
]);
SomeClass::doSomethingThatTriggersTheEvent();
Event::assertDispatched(function (MyEvent $event) {
return $event->my_value == 'something-to-check';
});
But when I can't trigger the logic directly and need to do it through the API call it doesn't work.
I'm trying to find the best way to test if an event is triggered when a specific endpoint of my API is requested.
I'm trying something like this:
Event::fake([
MyEvent::class,
]);
Http::get('http://localhost/my-path');
Event::assertDispatched(function (MyEvent $event) {
return $event->my_value == 'something-to-check';
});
I guess, that is fails, has something to do with the fact that the process that triggers the get call is a different process than the one of the test.
Currently I'm thinking of creating an instance of the controller and calling it the hard way, something like this:
Event::fake([
MyEvent::class,
]);
MyPathController::show(new Request);
Event::assertDispatched(function (MyEvent $event) {
return $event->my_value == 'something-to-check';
});
I doubt this would be the best solution.
I hope someone has a better solution here, thanks!
Please or to participate in this conversation.