framework: expectsEvents not working according to the documentation
I have the following test case…
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class UserEmailNotificationsTest extends \TestCase
{
use DatabaseTransactions;
public function testMemberNewCommentEmailNotification()
{
$community = factory(Community::class)->create();
$admin = factory(User::class, 'superadmin')->make();
$admin->community_id = $community->id;
$admin->save();
$asset = factory(Asset\Video::class)->make();
$asset->community_id = $community->id;
$asset->user_id = $admin->id;
$asset->save();
$user = factory(User::class)->make();
$user->community_id = $community->id;
$user->created_by = $admin->id;
$user->save();
$group = factory(Group::class)->make();
$group->created_by = $admin->id;
$group->community_id = $community->id;
$group->save();
$groupMedia = factory(GroupMedia::class)->make();
$groupMedia->asset_id = $asset->id;
$groupMedia->user_id = $user->id;
$groupMedia->group_id = $group->id;
$this->expectsEvents('eloquent.saving: GroupMedia');
$groupMedia->save();
$groupUser = factory(GroupUser::class)->make();
$groupUser->user_id = $user->id;
$groupUser->group_id = $group->id;
$groupUser->created_by = $admin->id;
$groupUser->save();
$this->expectsEvents('eloquent.created: GroupUser');
$comment = factory(Comment::class)->make();
$comment->media_id = $groupMedia->id;
$comment->user_id = $user->id;
$comment->save();
$this->expectsEvents('eloquent.created: Comment');
}
}
According to the documentation $this->expectsEvents()
should verify that the events are fired but also stop it from running the event handler callback.
I kept getting an error that the event was not being fired so I went to the event handler code and added an echo statement. I then ran my unit tests again. This time I saw the echo from the event handler and it still stated the event was not being fired.
➜ l5_media_communities git:(laravel-5.2-testing) ✗ phpunit
PHPUnit 5.2.10 by Sebastian Bergmann and contributors.
.E 2 / 2 (100%)
YES THE EVENT HANDLER HAS BEEN EXECUTED!
Time: 1.07 seconds, Memory: 27.25Mb
There was 1 error:
1) UserEmailNotificationsTest::testMemberNewCommentEmailNotification
Exception: These expected events were not fired: [eloquent.saving: GroupMedia]
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MocksApplicationServices.php:44
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:127
FAILURES!
Tests: 2, Assertions: 1, Errors: 1.
As you can see the uppercased string is coming from my event subscriber method that is triggered when the eloquent.saving: GroupMedia event is fired.
The second paragraph in the documentation is where I saw that the event handler should not be executed.
The BIGGER issue I am having is being told that the event did not fire when it actually did, with it reporting like that my tests always fail.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 1
- Comments: 16 (10 by maintainers)
http://stackoverflow.com/questions/36090425/laravel-5-2-event-testing-expectsevent-not-seeing-the-event-fired-although-it-i#comment60076813_36090425
Same thing happened to me,
$this->expectsEvent()
was not detecting that the event was being fired or prevent it from propagating to the event listeners…Previously, I was firing my events using
Event::fire(new Event())
. I tried changing it toevent(new Event())
and the test suddenly works correctly now, with it detecting that the event has been fired and silencing the event.Ping @taylorotwell. This seems like we could definitely improve this for L5.3.
@GrahamCampbell that event name is correct, you can see so in the source code…
Illuminate/Database/Eloquent/Model.php
It is an event thrown by Eloquent.
Also look at my latest comment before @arrilot I got past it and was using it improperly but there is still a bug here.