Symfony 邮件程序测试失败,调用成员函数getSubject()时为null

Symfony 邮件程序测试失败,调用成员函数getSubject()时为null,symfony,mailer,Symfony,Mailer,在Symfony 5.0.2项目中,新邮件程序的测试失败 错误:在null上调用成员函数getSubject() 电子邮件和电子邮件都基于symfonycast教程 添加var\u转储($email)显示对象(Symfony\Bridge\Twig\Mime\TemplatedEmail)#24(11){…,表示在服务中创建了一个真实的对象 services.yaml: App\Services\EmailerService: $mailer: '@mailer'

在Symfony 5.0.2项目中,新邮件程序的测试失败

错误:在null上调用成员函数getSubject()

电子邮件和电子邮件都基于symfonycast教程

添加
var\u转储($email)$email=…]之后立即在服务中添加code>显示
对象(Symfony\Bridge\Twig\Mime\TemplatedEmail)#24(11){…
,表示在服务中创建了一个真实的对象

services.yaml:

    App\Services\EmailerService:
        $mailer: '@mailer'
        $senderAddress: '%app.sender_address%'
服务:

use Symfony\Bridge\Twig\Mime\TemplatedEmail;

class EmailerService
{
    private $mailer;
    private $sender;

    public function __construct($mailer, $senderAddress)
    {
        $this->mailer = $mailer;
        $this->sender = $senderAddress;
    }

    public function appMailer($mailParams)
    {
        $email = (new TemplatedEmail())
                ->from($this->sender)
                ->to($mailParams['recipient'])
                ->subject($mailParams['subject'])
                ->htmlTemplate($mailParams['view'])
                ->context($mailParams['context']);
        $this->mailer->send($email);
    }
}
测试:

appMailer()
必须返回一个
TemplatedEmail
对象,以便您可以对其调用
getSubject()
。当前它不返回任何内容。将其更改为:

public function appMailer($mailParams)
{
    $email = (new TemplatedEmail())
            ->from($this->sender)
            ->to($mailParams['recipient'])
            ->subject($mailParams['subject'])
            ->htmlTemplate($mailParams['view'])
            ->context($mailParams['context']);
    $this->mailer->send($email);

    return $email; // I added this line. 
}

在这里,我认为无论是谁创建了symfonycast视频服务,他都知道一些我不知道的事情!简直不敢相信你读到的所有东西。谢谢。不客气!很容易漏掉一行。在这种情况下,我倾向于尝试从错误中“返回步骤”。即查找发生这种情况的地方,然后返回变量的创建位置(在本例中)
public function appMailer($mailParams)
{
    $email = (new TemplatedEmail())
            ->from($this->sender)
            ->to($mailParams['recipient'])
            ->subject($mailParams['subject'])
            ->htmlTemplate($mailParams['view'])
            ->context($mailParams['context']);
    $this->mailer->send($email);

    return $email; // I added this line. 
}