Agoi's avatar
Level 1

Assert Email was sent sends email and test fails

I have a method that creates a new user, sends welcome email and verification email like below:

    protected function createNewUser()
    {
        $user = $this->createUser();
        Mail::to($user)->send(new Welcome($user));
        Mail::to($user)->send(new ConfirmEmail($user));
        $this->createProfileFor($user);
        $this->createUserVerification($user);

        return $user;
    }

Which actually does send the email as I can see it in mailtrap.io, I wrote a test to assert an email was sent but shouldn't necessarily send the email like below:

    public function testHasErrorWhenEmailExists()
    {
        Mail::fake();
        $this->withoutExceptionHandling();

        $user = factory(\App\User::class)->create([
            'email' => '[email protected]'
        ]);

        $response = $this->post('/api/signup/store', [
          'password' => 'abc',
          'email' => $user->email,
          'lastname' => 'some lastname',
          'firstname' => 'some firstname',
        ])->assertSuccessful();

        Mail::assertSent(Welcome::class);

        $response
            ->assertStatus(200)
            ->assertJson([
                'status' => 422,
            ]);
    }

But the test keeps failing with below message:

There was 1 failure:

1) Tests\Feature\SignUpTest::testHasErrorWhenEmailExists
The expected [App\Mail\Welcome] mailable was not sent.
Failed asserting that false is true.

And the email is actually been sent.

Please what am I doing wrong as I believe an email shouldn't be sent and the test should pass?

0 likes
1 reply
Agoi's avatar
Level 1

Oh I got it all right, it was a typo.

Please or to participate in this conversation.