Framework: expectsEvents not working according to the documentation

Created on 12 Mar 2016  Â·  16Comments  Â·  Source: laravel/framework

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.

All 16 comments

I have also tried moving the call to $this->expectsEvents('eloquent.saving: GroupMedia') to be above the $groupMedia->save() call and it still reports that the event was not fired, and it also still executes the event handler code.

I was improperly using expectsEvents as it should only be called once with an array of events being passed into it. Now the only problem seems to be that expectsEvents doesn't handle the until method calls.

➜  l5_media_communities git:(laravel-5.2-testing) ✗ phpunit
PHPUnit 5.2.10 by Sebastian Bergmann and contributors.

.E                                                                  2 / 2 (100%)

Time: 448 ms, Memory: 24.50Mb

There was 1 error:

1) UserEmailNotificationsTest::testMemberNewCommentEmailNotification
BadMethodCallException: Method Mockery_0_Illuminate_Contracts_Events_Dispatcher::until() does not exist on this mock object

/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:221
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/watson/validating/src/ValidatingObserver.php:77
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/watson/validating/src/ValidatingObserver.php:77
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/watson/validating/src/ValidatingObserver.php:47
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/watson/validating/src/ValidatingObserver.php:20
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:348
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:221
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:164
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1679
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1466
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/app/library/AcceptsNestedAttributesFor.php:64
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:87
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/tests/UserEmailNotificationsTest.php:14

FAILURES!
Tests: 2, Assertions: 1, Errors: 1.

Are you sure that 'eloquent.saving: GroupMedia' is the correct event name? I don't quite remember but it seems to me that there should be a FQCN of a model.

All events were renamed in 5.2. That event name isn't correct.

@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.

@GrahamCampbell no, not all events have been renamed in 5.2.

@arrilot In this particular project, his models are not namespaced.

I think it only works with the object events then?

Ok so it is half baked and expectsEvents can only be used to test your own class based events? Will this method be refactored so that in the future it will support internal string based events or is the plan to get all laravel 5 events moved over to be class based?

Ping @taylorotwell. This seems like we could definitely improve this for L5.3.

I dont think it's a matter of classes vs strings... The mock is just checking for fire and not until.

I dont think it's a matter of classes vs strings

Yeh, that's why I re-opened it.

I was just making the point that I'd like to see more object events for consistency anyway, but that's unrelated.

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 to event(new Event()) and the test suddenly works correctly now, with it detecting that the event has been fired and silencing the event.

Closing since this has been changed in 5.3.x

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PhiloNL picture PhiloNL  Â·  3Comments

YannPl picture YannPl  Â·  3Comments

digirew picture digirew  Â·  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  Â·  3Comments

ghost picture ghost  Â·  3Comments