Reputation: 609
I have a Mail fie called EmailGenerator
and I have test called NotificationTest
.
Everytime I run the tests, I'm always getting this error.
Object of class App\Mail\EmailGenerator could not be converted to string
Here's my 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));
}
}
I just copied the sample code from this documetation https://laravel.com/docs/8.x/mocking#mail-fake
And here's my code in 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');
}
}
Upvotes: 1
Views: 1023
Reputation: 320
Looks like Mail::assertSent
is expecting the class name not an instance of the class.
Maybe change to:
Mail::assertSent(EmailGenerator::class, 1);
Upvotes: 2