gpt4 book ai didi

Laravel 5.2 事件测试 : expectsEvent not seeing the event fired although it is being fired

转载 作者:行者123 更新时间:2023-12-04 10:15:50 27 4
gpt4 key购买 nike

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

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

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

这是在项目上运行 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));
});
}

最佳答案

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

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

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

关于Laravel 5.2 事件测试 : expectsEvent not seeing the event fired although it is being fired,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36090425/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com