Framework: AnonymousNotifiable notifications cannot be tested using Notification::fake()

Created on 3 Jun 2018  路  1Comment  路  Source: laravel/framework

  • Laravel Version: 5.6.15
  • PHP Version: 7.1

Description:

When using "On-Demand Notifications" a AnonymousNotifiable is created and passed to the Notification system.
When in test using Notification Fakes or the notificationsFor method expects $notifiable to be a Notifiable class but it will be a string.

ErrorException: get_class() expects parameter 1 to be object, string given
vendor/laravel/framework/src/Illuminate/Support/Testing/Fakes/NotificationFake.php

Steps To Reproduce:

Use Notification::route('mail', '[email protected]')->notify(new InvoicePaid($invoice)); to send a notification
write a test that triggers that function
use the Notification Fake, Notification::fake();
the above error is produced

* Looks like there was a similar issue in the past that may have crept back in #20936 *

Most helpful comment

Ignore this. Looks like there is an example of it working in PR to fix older issue:
https://github.com/laravel/framework/pull/21379/commits/5ee629cb2bf993a2102c35404803c74ddbfeddae

$emailSentTo = '[email protected]';

Notification::assertSentTo(
    new AnonymousNotifiable(),
    WhateverNotificationYourTesting::class,
    function ($notification, $channels, $notifiable) use ($emailSentTo)  {
        return $notifiable->routes['mail'] === $emailSentTo;
    }
);

>All comments

Ignore this. Looks like there is an example of it working in PR to fix older issue:
https://github.com/laravel/framework/pull/21379/commits/5ee629cb2bf993a2102c35404803c74ddbfeddae

$emailSentTo = '[email protected]';

Notification::assertSentTo(
    new AnonymousNotifiable(),
    WhateverNotificationYourTesting::class,
    function ($notification, $channels, $notifiable) use ($emailSentTo)  {
        return $notifiable->routes['mail'] === $emailSentTo;
    }
);
Was this page helpful?
0 / 5 - 0 ratings