samedi 11 novembre 2017

laravel tests when mocking mail, however mail get delivered when running an end-to-end test

Going by the laravel documentation you use Mail::fake() importing Illuminate\Support\Facades\Mail to test behaviour in your tests however, my tests fail when I mock mail, if I run the flow from a controller, using the ruby gem mailcatcher for an SMTP server, I can see the mails delivered in the web ui of mailcatcher

here's the test case code, I tried two different assertions after failing to understand what's going wrong

<?php

namespace Tests\Unit;

use Tests\TestCase;
use App\Entities\Requester;
use App\Mail\RequesterRegistered;
use Illuminate\Support\Facades\Mail;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class RequesterRegisteredTest extends TestCase {

  use RefreshDatabase;

  public function setUp()
  {
    parent::setUp();
    Mail::fake();
  }

  public function testRequesterCreationTriggersEmail(){

    $expected = factory(Requester::class)->create();

    //assert email was to the company employee
    Mail::assertSent(RequesterRegistered::class, function ($mail) use ($expected) {
       return $mail->requester()->email_id === $expected->email_id;
    });

    // Assert a message was sent to the given users...
    Mail::assertSent(RequesterRegistered::class, function ($mail) use ($expected) {
      return $mail->hasTo($expected->email_id);
    });
  }
}

I have the observer declared in event service class

public function boot()
{
    parent::boot();

    Requester::observe(RequesterObserver::class);
}

and the observer has a created method that calls this mailer

public function created(Requester $requester)
{
    Log::debug("Sending registration email to :" . $requester->email_id);
    $message = (new RequesterRegistered($requester))->onQueue('import-mailer');
    Mail::to($requester->email_id)->queue($message);
}

here's the test result

MacBook-Pro-3:ctd-bo anadi$ ./vendor/bin/phpunit --filter RequesterRegisteredTest
PHPUnit 6.4.3 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 7.72 seconds, Memory: 16.00MB

There was 1 failure:

1) Tests\Unit\RequesterRegisteredTest::testRequesterCreationTriggersEmail
The expected [App\Mail\RequesterRegistered] mailable was not sent.
Failed asserting that false is true.

/Users/anadi/Code/CTD/ctd-bo/vendor/laravel/framework/src/Illuminate/Support/Testing/Fakes/MailFake.php:41
/Users/anadi/Code/CTD/ctd-bo/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:221
/Users/anadi/Code/CTD/ctd-bo/tests/Unit/RequesterRegisteredTest.php:29

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

not to mention a similar event test for this observer also fails if happen to use Event::fake()

I am using sync as queue connection for testing environment and local development.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2zvIopy
via IFTTT

Aucun commentaire:

Enregistrer un commentaire