Jos*_*ord 7 laravel laravel-5.2
我一直试图测试事件,我昨天工作了.那是在我开始重构测试代码以防止它过于重复之前.我添加了setUp方法调用以使用ModelFactories生成伪数据.这是在昨天的每个测试案例中完成的,如上所述它正在工作.
我认为这与使用setUp方法有关,但我不知道为什么会这样.首先,我尝试使用setUpBeforeClass()静态方法,因为它只在单元测试运行时运行一次.但是,在第一次调用setUp()之前,实际上并没有设置laravel应用程序......可能的错误可能是什么?这篇文章记录在Laravel中设置PHPUnit测试.
因此,我选择使用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.
Run Code Online (Sandbox Code Playgroud)
这是我创建的单元测试文件的代码.
<?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.....
}
}
Run Code Online (Sandbox Code Playgroud)
有什么想法,为什么它没有看到事件被解雇?我觉得奇怪的是,如果我在测试用例中创建模型但是在内部完成时似乎失败了它们会被解雇setUp().最糟糕的是我没有在setUp方法中创建GroupMedia模型,而是在测试用例中完成.
我还转储了从getRandomCommunityWithAssociatedData方法返回的数据,它返回了所有id属性的正确模型对象,告诉我它们在创建过程中都保存到了数据库中.
这里要求的是实际触发事件的代码,它位于静态引导方法的GroupMedia模型中.
protected static function boot()
{
parent::boot();
static::saving(function($groupMedia) {
Event::fire(new \App\Events\GroupMediaSaving($groupMedia));
});
}
Run Code Online (Sandbox Code Playgroud)
bee*_*bug 12
如果查看expectsEvents(在特征内Illuminate/Foundation/Testing/Concerns/MocksApplicationServices)的源代码,您将看到它调用函数withoutEvents,该函数模拟应用程序事件调度程序,抑制并收集所有未来事件.
您遇到的问题是此时setUp已经调用了该函数,因此测试不会捕获并记录您的事件,并且在评估断言时不会显示.
为了正确地查看事件触发,您应该确保在触发事件的代码之前声明断言.
同样的事情发生在我身上,$this->expectsEvent()没有检测到事件正在被触发或阻止它传播到事件侦听器。
以前,我使用 来触发事件Event::fire(new Event())。我尝试将其更改为event(new Event()),测试现在突然正常工作,它检测到事件已被触发并静音该事件。
| 归档时间: |
|
| 查看次数: |
1836 次 |
| 最近记录: |