Reputation: 71
I am creating a test for Laravel's built-in email verification and am unsure how to test this via PHPUnit. I'm able to receive the notification emails using mailtrap.io but am unable to make the PHPUnit test pass.
Here is my test:
namespace Tests\Feature;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class RegistrationTest extends TestCase
{
use RefreshDatabase;
function test_a_confirmation_email_is_sent_on_registration()
{
Notification::fake();
$user = create('App\User');
Notification::assertSentTo($user, VerifyEmail::class);
}
}
I'm looking to have the assertSentTo to pass.
Right now I am getting:
The expected [Illuminate\Auth\Notifications\VerifyEmail] notification was not sent. Failed asserting that false is true. /home/jhiggins/projects/forum/vendor/laravel/framework/src/Illuminate/Support/Testing/Fakes/NotificationFake.php:52 /home/jhiggins/projects/forum/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:237 /home/jhiggins/projects/forum/tests/Feature/RegistrationTest.php:20
Upvotes: 4
Views: 2276
Reputation: 9
When a new user is registered, Laravel (5.7 and above) dispatches a Registered Event, so I think the main point here would be to assert that event has been called. The rest of the work (sending email verification, etc.) is already handled by the Laravel Framework.
Here is a snippet of my feature test:
public function a_confirmation_email_is_sent_upon_registration()
{
Event::fake();
$this->post(route('register'), [
'name' => 'Joe',
'email' => '[email protected]',
'password' => 'passwordtest',
'password_confirmation' => 'passwordtest'
]);
Event::assertDispatched(Registered::class);
}
Upvotes: 0