【发布时间】:2017-07-26 09:49:55
【问题描述】:
我正在做一些单元测试,想测试一些基本的东西。在我的测试中,我使用Illuminate\Foundation\Testing\WithoutEvents。
当用户注册时,他或她会收到一封激活邮件。首先,我为此使用了一个观察者,但得出的结论是,当使用 WithoutEvents 写为 here 和 here 时,Laravel 不会禁用观察者。然后我将代码更改为“传统”事件和侦听器。
EventServiceProvider 仍然是默认的,除了$listen 属性:
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\UserCreated' => [
'App\Listeners\CreateActivation'
],
];
当我发送事件时:
event(new UserCreated($user));
还有一个示例测试(失败):
class ExampleTest extends TestCase
{
use DatabaseMigrations, WithoutEvents;
public function testExample()
{
$user = factory(User::class)->create();
}
}
错误:
我不知道它为什么会崩溃。因为我不确定问题出在哪里。
【问题讨论】:
-
尝试在你的测试用例中添加
$this->withoutEvents();$user = factory(User::class)->create();之前!! -
我已经在使用
WithoutEventstrait,它相当于$this->withoutEvents(),但用于所有测试。
标签: php laravel unit-testing laravel-5