阅读(2926) (7)

Laravel 8 任务模拟

2021-07-08 16:55:51 更新

作为模拟的替代方式,你可以使用 Bus Facade 的 fake 方法来防止任务被真正分发执行。使用 fake 的时候,断言一般出现在测试代码的后面:

<?php

namespace TestsFeature;

use AppJobsShipOrder;
use IlluminateFoundationTestingRefreshDatabase;
use IlluminateFoundationTestingWithoutMiddleware;
use IlluminateSupportFacadesBus;
use TestsTestCase;

class ExampleTest extends TestCase
{
    public function testOrderShipping()
    {
        Bus::fake();

        // 执行订单发货...

        // 断言已完成特定类型的工作,以满足给定的真实性测试...
        Bus::assertDispatched(function (ShipOrder $job) use ($order) {
            return $job->order->id === $order->id;
        });

        // 断言任务并未分发...
        Bus::assertNotDispatched(AnotherJob::class);
    }
}