【问题标题】:Object of class App\Mail\EmailGenerator could not be converted to stringApp\Mail\EmailGenerator 类的对象无法转换为字符串
【发布时间】:2021-06-16 06:02:00
【问题描述】:

我有一个名为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');
    }
}

【问题讨论】:

    标签: php laravel phpunit tdd laravel-dusk


    【解决方案1】:

    看起来Mail::assertSent 期望的是类名而不是类的实例。

    也许改成:

    Mail::assertSent(EmailGenerator::class, 1);
    

    【讨论】:

      猜你喜欢
      • 2015-11-19
      • 2018-03-18
      • 2020-01-08
      • 2023-01-05
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 2015-07-27
      相关资源
      最近更新 更多