vendredi 26 janvier 2018

Laravel 5 testing even-listener sending email

I'd like some help as I don't have any experience with writing tests. I'm having this method in my controller

// controller
public function createNew(Request $request)
{


        $foo = new Foo($request->only([
            'bar', 
            'baar', 
            'baaar',
        ]));

        $foo->save();

        // sending email once new record is created
        event(new FooEvent($foo));


        return redirect()->route('/');
}

class FooEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $foo;

    public function __construct(Foo $foo)
    {
        $this->foo = $foo;
    }
}

class EmailFooListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }


    public function handle(FooEvent $event)
    {
        \Mail::to($event->foo->email)->send(new FooEmail($event->foo));
    }
}


$factory->define(Blubolt\Foo::class, function (Faker $faker) {
    return [
        'bar' => $faker->bar,
        'baar'     => $faker->baar,
        'baaar'      => $faker->baaar,
    ];
});

How can I write a test in order to test the even-listener for sending emails that works properly? No need to send actual emails, just write a test that makes my code pass



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire