如何解决类 App\Mail\EmailGenerator 的对象无法转换为字符串
我有一个名为 EmailGenerator
的邮件文件,我有一个名为 NotificationTest
的测试。
每次运行测试时,总是出现此错误。
Object of class App\Mail\EmailGenerator could not be converted to string
这是我的NotificationTest.php
<?php
namespace Tests\Unit;
use Tests\TestCase;
use App\Http\Livewire\Notifications\CreateNotification;
use App\Http\Livewire\Notifications\ManageNotifications;
use App\Models\EmailTemplate;
use Livewire\Livewire;
use Mockery;
use Mockery\MockInterface;
use Mail;
use App\Mail\EmailGenerator;
use App\Models\User;
class NotificationTest extends TestCase
{
public function test_sample_email()
{
Mail::fake();
Mail::assertNothingSent();
$template = EmailTemplate::factory()->create([
'name' => 'foo',]);
Mail::assertSent(new EmailGenerator($template));
}
}
我刚刚从这个文档中复制了示例代码 https://laravel.com/docs/8.x/mocking#mail-fake
这是我在 EmailGenerator.php
中的代码
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Models\EmailTemplate;
class EmailGenerator extends Mailable
{
use Queueable,SerializesModels;
public $template;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(EmailTemplate $template)
{
$this->template = $template;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject($this->template->subject)
->markdown('emails.generator');
}
}
解决方法
看起来 Mail::assertSent
期望的是类名而不是类的实例。
也许会改为:
Mail::assertSent(EmailGenerator::class,1);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。