laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.51k stars 11.02k forks source link

Testing: assertSent does not count as an assertion in PHPUnit #18349

Closed perkola closed 7 years ago

perkola commented 7 years ago

Description:

When mocking mail using Mail::assetSent, PHPUnit flags the test case as risky with the following explanation:

This test did not perform any assertions

Steps To Reproduce:

The following test case was used:

public function testOrderShipping()
{
    Mail::fake();

    // creates an order and send the OrderShipped mailable
    $this->json('POST', '/orders', ['name' => 'Sally']);

    Mail::assertSent(OrderShipped::class, function ($mail) {
        return $mail->order->name === 'Sally';
    });
}

The OrderShipped mailable is invoked on the route /orders with:

Mail::to('test@test.com')->send(new OrderShipped($order));

Without sending the JSON request to the API the mailable is never sent and PHPUnit yield the following:

The expected [App\Mail\OrderShipped] mailable was not sent.

This expects me to believe that the assertSent method never results in a proper assertion required by PHPUnit as the mail seems to be sent and the test case passes as risky.

themsaid commented 7 years ago

That's what happens inside assertSent:

    public function assertSent($mailable, $callback = null)
    {
        PHPUnit::assertTrue(
            $this->sent($mailable, $callback)->count() > 0,
            "The expected [{$mailable}] mailable was not sent."
        );
    }

A proper assertion is used as you can see, and PHPUnit returns:

The expected [App\Mail\TestMail] mailable was not sent.
Failed asserting that false is true.

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
themsaid commented 7 years ago

Closing for lack of activity and being unable to replicate.