Laravel 预期的[App\Mail\WelcomeEmail]可邮件未发送。断言false为true失败
我哪里出错了?blow代码不适用于测试。 找到用户。没有像user null这样的问题 错误消息是:Laravel 预期的[App\Mail\WelcomeEmail]可邮件未发送。断言false为true失败,laravel,laravel-7,phpunit,Laravel,Laravel 7,Phpunit,我哪里出错了?blow代码不适用于测试。 找到用户。没有像user null这样的问题 错误消息是: The expected [App\Mail\WelcomeEmail] mailable was not sent. Failed asserting that false is true. at vendor/laravel/framework/src/Illuminate/Support/Testing/Fakes/MailFake.php:64 60| }
The expected [App\Mail\WelcomeEmail] mailable was not sent. Failed asserting that false is true.
at vendor/laravel/framework/src/Illuminate/Support/Testing/Fakes/MailFake.php:64
60| }
61|
62| PHPUnit::assertTrue(
63| $this->sent($mailable, $callback)->count() > 0,
> 64| $message
65| );
66| }
67|
测试类
class MailTest extends TestCase
{
/**
* A basic feature test example.
*
* @return void
*/
public function testWelcomeMail()
{
$this->withoutExceptionHandling();
Mail::fake();
Mail::assertSent(WelcomeEmail::class);
}
}
class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;
public $data;
public function __construct($data)
{
$this->data = $data;
}
public function build()
{
$user = User::find(3425);
$address = 'myjourney@asdasd.com';
$subject = __('email.welcome_to_journey');
$name = __('email.my_journey');
if ($user != NULL) {
\App::setLocale($user->lang);
$user->is_welcome_email_sent = 1;
$user->save();
}
return $this->view('email.user_welcome', ['user' => $user])
->from($address, $name)
// ->cc($address, $name)
// ->bcc($address, $name)
->replyTo($address, $name)
->subject($subject);
}
}
邮件类
class MailTest extends TestCase
{
/**
* A basic feature test example.
*
* @return void
*/
public function testWelcomeMail()
{
$this->withoutExceptionHandling();
Mail::fake();
Mail::assertSent(WelcomeEmail::class);
}
}
class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;
public $data;
public function __construct($data)
{
$this->data = $data;
}
public function build()
{
$user = User::find(3425);
$address = 'myjourney@asdasd.com';
$subject = __('email.welcome_to_journey');
$name = __('email.my_journey');
if ($user != NULL) {
\App::setLocale($user->lang);
$user->is_welcome_email_sent = 1;
$user->save();
}
return $this->view('email.user_welcome', ['user' => $user])
->from($address, $name)
// ->cc($address, $name)
// ->bcc($address, $name)
->replyTo($address, $name)
->subject($subject);
}
}
我可以使用其他测试文件,如登录、注册等,但邮寄没有。是否确实有id为3425的用户
$user = User::find(3425);
您的代码可能在这里中断,因为我们无法从空对象检索lang。这一行可能应该删除,因为它已经在下面检查用户是否存在的行上使用
\App::setLocale($user->lang);
您的电子邮件模板似乎也使用来自$user的数据,因为user对象被传递给模板。如果您希望该用户存在,这也可能会中断。您没有将电子邮件与其他凭据一起发送。你的函数应该是这样的
public function testWelcomeMail()
{
$this->withoutExceptionHandling();
Mail::fake();
Mail::to('<email_address>')->send(new WelcomeEmail(['user' => <some_number>]));
Mail::assertSent(WelcomeEmail::class);
}
公共函数testWelcomeMail()
{
$this->withoutExceptionHandling();
邮件::假();
邮件::发送到(“”)->发送(新的欢迎电子邮件(['user'=>]);
Mail::assertSent(WelcomeEmail::class);
}
该id属于实际用户。通常情况下,代码在项目中工作。您在执行测试时是否使用相同的数据库?您的测试类正在使用RefreshDatabase,它在测试时清除您的数据库。除非我弄错了,否则如果在测试开始时没有添加用户,那么应该没有用户。是的,我正在使用相同的数据库来测试它。我已经有了另一个测试类,可以用creadiantials登录,并且可以正常工作。您好。。。请不要张贴代码图片,代码是文字