Laravel集成测试工作


Laravel integration testing jobs

我正试图为我的应用程序运行集成测试。我有这些工作:

  • StartJob
  • PrepareJob
  • PeformJob

StartJob调度一个或多个PrepareJob,每个PrepareJob调度一个PerformJob。

添加

$this->expectsJobs(
        [
            StartJobs::class,
            PrepareJob::class,
            PerformJob::class
        ]
    );

使我的测试失败,错误提示

1) JobsTest::testJobs
BadMethodCallException: Method Mockery_0_Illuminate_Contracts_Bus_Dispatcher::dispatchNow() does not exist on this mock object

删除$this->expectsJobs使我的所有测试都通过,但我不能断言给定的作业已经运行,只能断言它是否将DB修改为给定的状态。

StartJobs.php

    class StartJobs extends Job implements ShouldQueue
    {
        use InteractsWithQueue;
        use DispatchesJobs;
        public function handle(Writer $writer)
        {
            $writer->info("[StartJob] Started");
            for($i=0; $i < 5; $i++)
            {
                $this->dispatch(new PrepareJob());
            }
            $this->delete();
        }
    }

PrepareJob.php

class PrepareJob  extends Job implements ShouldQueue
{
    use InteractsWithQueue;
    use DispatchesJobs;
    public function handle(Writer $writer)
    {
        $writer->info("[PrepareJob] Started");
        $this->dispatch(new PerformJob());
        $this->delete();
    }
}

PerformJob.php

class PerformJob  extends Job implements ShouldQueue
{
    use InteractsWithQueue;
    public function handle(Writer $writer)
    {
        $writer->info("[PerformJob] Started");
        $this->delete();
    }
}

JobsTest.php

class JobsTest extends TestCase
{
    /**
     * @var Dispatcher
     */
    protected $dispatcher;
    protected function setUp()
    {
        parent::setUp();
        $this->dispatcher = $this->app->make(Dispatcher::class);
    }
    public function testJobs()
    {
        $this->expectsJobs(
            [
                StartJobs::class,
                PrepareJob::class,
                PerformJob::class
            ]
        );
        $this->dispatcher->dispatch(new StartJobs());
    }
}

我认为它与我如何使用具体的调度程序有关,而$this->expectsJob嘲笑调度程序。可能和这个有关- https://github.com/laravel/lumen-framework/issues/207。怎么解呢?

对我来说,听起来好像没有dispatchNow()方法。在Jobs中运行dispatch(),但是错误提示dispatchNow()不存在。

Laravel没有dispatchNow()-方法之前的某个版本(我认为Laravel 5.2…不确定),但只是调度()。可能是乔布斯没有考虑到这一点而失败了。

你可以试着不在一个数组中传递它,而是使用3个命令:

$this->expectsJobs(StartJobs::class);
$this->expectsJobs(PrepareJob::class);
$this->expectsJobs(PerformJob::class);

也许有帮助。