Laravel 5.2事件测试:expectEvent虽然被解雇但没有看到事件被触发

时间:2016-03-18 17:12:58

标签: laravel laravel-5.2

我一直在尝试测试事件,我昨天开始工作。那是在我开始重构测试代码以防止它过于重复之前。我添加了setUp方法调用以使用ModelFactories生成伪数据。这是在昨天的每个测试案例中完成的,如上所述它正在工作。

我认为这与使用setUp方法有关,但我不知道为什么会这样。首先,我尝试使用setUpBeforeClass()静态方法,因为它只在单元测试运行时运行一次。但是,在第一次调用setUp()之前,实际上并没有设置laravel应用程序......可能的错误可能是什么?这篇SO帖子Setting up PHPUnit tests in Laravel中有记载。

因此我选择使用setUp方法,只是检查静态属性是否为null,如果它为null则生成数据,如果不是,它就会继续运行。

以下是在项目上运行phpunit的输出

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

E                                                                  1 / 1 (100%)

Time: 8.94 seconds, Memory: 33.50Mb

There was 1 error:

1) UserEmailNotificationsTest::testNotificationSentOnGroupMediaSaving
Exception: These expected events were not fired: [\App\Events\GroupMediaSaving]

/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: 1, Assertions: 0, Errors: 1, Skipped: 8.

这是我创建的单元测试文件的代码。

<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class UserEmailNotificationsTest extends \TestCase
{
    use DatabaseTransactions;

    const COMMUNITIES_TO_CREATE = 3;
    const USERS_TO_CREATE = 10;
    const ADMINS_TO_CREATE = 5;

    protected static $communities = null;
    protected static $users = null;
    protected static $admins = null;

    public function setUp()
    {
        parent::setUp(); // TODO: Change the autogenerated stub

        if(is_null(self::$communities)) {
            self::$communities = factory(\Community::class, self::COMMUNITIES_TO_CREATE)->create()->each(function ($community) {
                self::$users[$community->id] = factory(User::class, self::USERS_TO_CREATE)->create()->each(function (\User $user) use ($community) {
                    $user->community()->associate($community);
                    $user->save();
                });

                self::$admins[$community->id] = factory(User::class, self::ADMINS_TO_CREATE, 'superadmin')->create()->each(function (\User $admin) use ($community) {
                    $admin->community()->associate($community);
                    $admin->save();
                });

                $community->save();
            });
        }
    }

    public static function getRandomCommunityWithAssociatedData()
    {
        $community = self::$communities[mt_rand(0, count(self::$communities)-1)];
        return ['community' => $community, 'users' => self::$users[$community->id], 'admins' => self::$admins[$community->id]];
    }

    /**
     * Test that the notification event is fired when a group media
     * item is saved.
     */
    public function testNotificationSentOnGroupMediaSaving()
    {
        $data = self::getRandomCommunityWithAssociatedData();

        // FOR SOME REASON THIS SAYS THE EVENT IS NEVER FIRED WHEN IT ACTUALLY IS FIRED.
        $this->expectsEvents(['\App\Events\GroupMediaSaving']);

        $community = $data['community'];
        $admin = $data['admins'][0];
        $user = $data['users'][0];

        $asset = factory(Asset\Video::class)->make();
        $asset->community()->associate($community);
        $asset->user()->associate($admin);
        $asset->save();

        $group = factory(Group::class)->make();
        $group->community()->associate($community);
        $group->created_by = $admin->id;
        $group->save();


        $groupMedia = factory(GroupMedia::class)->make();
        $groupMedia->asset()->associate($asset);
        $groupMedia->user()->associate($user);
        $groupMedia->group()->associate($group);
        $groupMedia->published_date = date('Y-m-d H:i:s', strtotime('-1 day'));
        $groupMedia->save();

        // I can print_r($groupMedia) here and it does have an ID attribute so it was saved, I also put some debugging in the event object and it is actually fired.....
    }
}

为什么它没有看到事件被解雇的任何想法?如果我在测试用例中创建模型但在setUp()内完成时似乎失败了,我发现它们被解雇是奇怪的。最糟糕的是我没有在setUp方法中创建GroupMedia模型,而是在测试用例中完成。

我还转储了从getRandomCommunityWithAssociatedData方法返回的数据,它返回了所有id属性的正确模型对象,告诉我在创建过程中它们都保存到了数据库中。

这里要求的是实际触发事件的代码,它位于静态引导方法的GroupMedia模型中。

protected static function boot()
{
    parent::boot();

    static::saving(function($groupMedia) {
        Event::fire(new \App\Events\GroupMediaSaving($groupMedia));
    });
}

2 个答案:

答案 0 :(得分:9)

如果查看expectsEvents的源代码(在特征Illuminate/Foundation/Testing/Concerns/MocksApplicationServices内),您将看到它调用函数withoutEvents,该函数模拟应用程序事件调度程序,抑制和收集所有未来事件。

问题在于此时已经调用setUp函数,因此测试不会捕获和记录您的事件,并且在评估断言时不会显示。< / p>

为了正确地查看事件触发,您应该确保在触发事件的代码之前声明断言。

答案 1 :(得分:1)

同样的事情发生在我身上,$this->expectsEvent()没有检测到事件被触发或阻止它传播到事件监听器..

以前,我使用Event::fire(new Event())解雇了我的活动。我尝试将其更改为event(new Event()),现在测试突然正常,它检测到事件已被触发并使事件静音。