测试Laravel事件监听器

时间:2015-11-30 09:47:44

标签: laravel testing event-listener mockery

我正在尝试在Laravel中测试事件监听器。 这是我的倾听者:

class FlashNotifier
{

    public function handleMenuItemWasStored()
    {
        Session::flash('flash-status', 'success');
        Session::flash('flash-message', 'Item was stored.');
    }

    public function subscribe($events)
    {
        $events->listen(
            MenuItemWasStored::class,
            '\App\Listeners\Menu\FlashNotifier@handleMenuItemWasStored'
        );
    }
}

这是我到目前为止的测试:

public function testEventListenerWasTriggered()
{
    $listener = Mockery::mock('App\Listeners\Menu\FlashNotifier');
    $d = new Dispatcher;
    $listener->shouldReceive('handleMenuItemWasStored')->once();
    $d->fire('App\Events\Menu\MenuItemWasStored');
}

但是,我得到以下例外:

1) FlashListenerTest::testEventListenerWasTriggered
Mockery\Exception\InvalidCountException: Method handleMenuItemWasStored() from Mockery_1_App_Listeners_Menu_FlashNotifier should be called
exactly 1 times but called 0 times.

我做错了什么?

0 个答案:

没有答案