为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel Notification Mailable 在测试中不起作用

Laravel Notification Mailable 在测试中不起作用

PHP
扬帆大鱼 2022-01-14 18:08:11
当通知被触发时,我正在使用通知向所有用户发送邮件。这在手动“浏览网站”使用中效果很好(向 ContactController 提交 POST),但在我的测试中,邮件门面声称没有发送任何邮件。控制器:<?phpnamespace App\Http\Controllers;use App\Notifications\ContactRequestNotification;use App\User;use Illuminate\Http\Request;use Notification;class ContactController extends Controller{    public function create(Request $request)    {        $validateData = $request->validate([            'name' => 'required',            'email' => 'required|email',            'message' => 'required'        ]);        Notification::send(User::all(), new ContactRequestNotification($validateData));        return $validateData;    }}通知:<?phpnamespace App\Notifications;use App\Mail\ContactMail;use Illuminate\Mail\Mailable;use Illuminate\Notifications\Notification;class ContactRequestNotification extends Notification{    /**     * @var array     */    private $contactData;    /**     * Create a new notification instance.     *     * @return void     */    public function __construct($contactData)    {        $this->contactData = $contactData;    }    /**     * Get the notification's delivery channels.     *     * @param mixed $notifiable     * @return array     */    public function via($notifiable)    {        return ['mail'];    }    /**     * Get the mail representation of the notification.     *     * @param mixed $notifiable     * @return Mailable     */    public function toMail($notifiable)    {        return (new ContactMail($this->contactData['name'], $this->contactData['email'], $this->contactData['message']))->to($notifiable->email);    }    /**     * Get the array representation of the notification.     *     * @param mixed $notifiable     * @return array     */    public function toArray($notifiable)    {        return $this->contactData;    }}在浏览器中执行相同的任务时,会向用户发送一封邮件。关于我在这里缺少什么的任何提示?互联网搜索显示其他一些人遇到了这个问题,但都没有找到解决方案(或者我无法执行正确的搜索)
查看完整描述

1 回答

?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

看起来MailChannel发送通知电子邮件的驱动程序没有使用Mail外观,这意味着Mail::fake不会影响它。相反,它直接调用该send方法,该方法又调用(邮件驱动程序)。MailablesendMailer

您可以将Mailable实例替换为MailFake(这是Mail::fake使用的)的实例,但它看起来MailFake不适合当$view是一个数组(这是MailChannel传递给 的内容Mailable)的情况。

幸运的是,Laravel 源代码包含一个示例,说明他们如何测试在SendingMailNotificationsTest. 他们模拟MailerandMarkdown实例并检查传递的参数。你可以做类似的事情:

use Mockery as m;

use Illuminate\Contracts\Mail\Mailable;

use Illuminate\Contracts\Mail\Mailer;

use Illuminate\Mail\Markdown;

use Illuminate\Mail\Message;


class ContactFormTest extends TestCase

{


    protected function tearDown(): void

    {

        parent::tearDown();

        m::close();

    }


    protected function setUp(): void

    {

        parent::setUp();


        $this->mailer = m::mock(Mailer::class);

        $this->markdown = m::mock(Markdown::class);

        $this->instance(Mailer::class, $this->mailer);

        $this->instance(Mailer::class, $this->markdown);

    }


    public function a_mail_is_send_when_the_contact_form_is_used()

    {

        $this->withExceptionHandling();


        $user = factory(User::class)->create();


        $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');


        $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');


        $data = [

            'name' => 'John Doe',

            'email' => 'email@email.com',

            'message' => 'This is a test message'

        ];


        $notification = new ContactRequestNotification($data);


        $this->mailer->shouldReceive('send')->once()->with(

            ['html' => 'htmlContent', 'text' => 'textContent'],

            array_merge($notification->toMail($user)->toArray(), [

                '__laravel_notification' => get_class($notification),

                '__laravel_notification_queued' => false,

            ]),

            m::on(function ($closure) {

                $message = m::mock(Message::class);

                $message->shouldReceive('to')->once()->with([$user->email]);

                $closure($message);

                return true;

            })

        );



        $response = $this->post('/contact', $data);


        $response->assertStatus(200);


    }

}


就个人而言,我现在宁愿只toMail对类上的方法进行单元测试,ContactRequestNotification因为我认为上面的方法不是很漂亮。


查看完整回答
反对 回复 2022-01-14
  • 1 回答
  • 0 关注
  • 242 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号