作业未在测试中分派
Job not being dispatched in test
我正在使用 laravel 8 和 Laravel Sail。
我正在尝试测试从工作中发送但未发送的电子邮件,无论我做什么。这是我的代码
Bus::fake();
Mail::fake();
TheProductDoesNotExists::dispatch($this->channel, $document['product'], $document['name']);
Event::assertDispatched(TheProductDoesNotExists::class);
Mail::assertSent(ProductMissing::class);
然后我得到
The expected [App\Mail\ProductMissing] mailable was not sent.
Failed asserting that false is true.
在作业中,我什至在 handle 方法中有一个记录器,但没有记录任何内容
public function handle()
{
logger('from the job');
$alertTo = 'test@test';
Mail::to($alertTo)->send(
new ProductMissing($this->product, $this->orderName)
);
}
什么也没有。任何帮助将非常感激!谢谢
当你写Queue::fake()
或Bus::fake()
时,框架会用一个SIMPLE ARRAY代替一个真正的队列(redis,数据库...)。所有作业都将存储在该数组中,并且不会执行。该数组用于以后的断言。所以在你的代码中:
Bus::fake();
Mail::fake();
TheProductDoesNotExists::dispatch($this->channel, $document['product'], $document['name']);
Event::assertDispatched(TheProductDoesNotExists::class);
Mail::assertSent(ProductMissing::class);
因为 TheProductDoesNotExists
甚至没有执行,所以没有捕获电子邮件,最后一行失败。
你只能测试这两个中的一个。
Bus::fake();
TheProductDoesNotExists::dispatch($this->channel, $document['product'], $document['name']);
Bus::assertDispatched(TheProductDoesNotExists::class);
或者:
Mail::fake()
TheProductDoesNotExists::dispatchNow($this->channel, $document['product'], $document['name']);
Mail::assertSent(ProductMissing::class);
不能同时进行。
为了更好地理解,我建议阅读 Laravel 源代码中的 Illuminate\Support\Testing\Fakes\QueueFake
。
我正在使用 laravel 8 和 Laravel Sail。
我正在尝试测试从工作中发送但未发送的电子邮件,无论我做什么。这是我的代码
Bus::fake();
Mail::fake();
TheProductDoesNotExists::dispatch($this->channel, $document['product'], $document['name']);
Event::assertDispatched(TheProductDoesNotExists::class);
Mail::assertSent(ProductMissing::class);
然后我得到
The expected [App\Mail\ProductMissing] mailable was not sent.
Failed asserting that false is true.
在作业中,我什至在 handle 方法中有一个记录器,但没有记录任何内容
public function handle()
{
logger('from the job');
$alertTo = 'test@test';
Mail::to($alertTo)->send(
new ProductMissing($this->product, $this->orderName)
);
}
什么也没有。任何帮助将非常感激!谢谢
当你写Queue::fake()
或Bus::fake()
时,框架会用一个SIMPLE ARRAY代替一个真正的队列(redis,数据库...)。所有作业都将存储在该数组中,并且不会执行。该数组用于以后的断言。所以在你的代码中:
Bus::fake();
Mail::fake();
TheProductDoesNotExists::dispatch($this->channel, $document['product'], $document['name']);
Event::assertDispatched(TheProductDoesNotExists::class);
Mail::assertSent(ProductMissing::class);
因为 TheProductDoesNotExists
甚至没有执行,所以没有捕获电子邮件,最后一行失败。
你只能测试这两个中的一个。
Bus::fake();
TheProductDoesNotExists::dispatch($this->channel, $document['product'], $document['name']);
Bus::assertDispatched(TheProductDoesNotExists::class);
或者:
Mail::fake()
TheProductDoesNotExists::dispatchNow($this->channel, $document['product'], $document['name']);
Mail::assertSent(ProductMissing::class);
不能同时进行。
为了更好地理解,我建议阅读 Laravel 源代码中的 Illuminate\Support\Testing\Fakes\QueueFake
。