mardi 16 janvier 2018

Testing assertDispatched only works when using event helper

Registering service provider:

public function register()
{
    $this->app->singleton(CartInterface::class, function ($app) {
        return new SessionCart($app['session'], $app['events']);
    });
}

Firing events within the service above:

$this->events->fire('cart.added', $item);

Testing the service:

public function it_can_add_an_item()
{
    Event::fake();
    $this->cartService->add(new Item);

    $this->assertEquals(1, $this->cartService->count());
    Event::assertDispatched('cart.added');
}

Result: The expected [cart.added] event was not dispatched.

If instead of using $this->events to fire events I just use the event helper like event('cart.added') I'm back to green.

I'm not dying for using the object oriented approach, but I'm really curious about the reason it doesn't work here, because the event helper seems to be using an instance of the dispatcher right from the container just like I do when registering the service.

Any clue?

Aucun commentaire:

Enregistrer un commentaire