我很难理解如何在另一份工作中测试一份工作.我将提供一个代码示例.
这是我的主要工作类,我们可以称之为父亲
final class FatherJob implements ShouldQueue { use Dispatchable,InteractsWithQueue,Queueable,SerializesModels; /** * Create a new job instance. */ public function __construct() { } /** * Execute the job. * * @return void */ public function handle() { \Log::info("Hello World,I'm the father."); dispatch(new ChildJob()); } }
然后我们有了孩子的工作
final class ChildJob implements ShouldQueue { use Dispatchable,SerializesModels; /** * Create a new job instance. */ public function __construct() { } /** * Execute the job. * * @return void */ public function handle() { \Log::info("I'm the child"); } }
测试设置如下
final class JobTest extends TestCase { /** @test */ public function it_has_been_dispatched() { $this->expectsJobs(ChildJob::class); dispatch(New FatherJob ()); } }
这个测试失败了,当然这是问题的全部要点,但为什么呢?
我已经做了一些挖掘,我认为问题依赖于在expectedJobs()内部调用withoutJobs(),似乎withoutJobs()破坏了当前队列,因此它不允许调用其余的工作,但也许我我完全偏离轨道.
如果打算使用此逻辑,我该如何创建一个测试套件,以便检查作业中的作业是否已被调用?
先感谢您.
解决方法
expectJobs模拟所有作业引擎.你不能使用dispacth().
$this->expectsJobs(ChildJob::class); $job = new FatherJob(); $job->handle();