【问题标题】:Assert Laravel Command is Called in Test断言 Laravel 命令在测试中被调用
【发布时间】:2020-01-28 17:34:17
【问题描述】:

我从 Laravel 命令调用 Laravel 命令。它可以工作,但我想测试是否调用了子命令 A 和/或 B

简化版以下:

final class HandleActions extends Command
{
    public function handle()
    {
        foreach ($items as $item) {
            if ($item->price * 2.24 > 100) {
                $this->call('order:reprocess'); // command A
            }

            if (!$item->stock) {
                $this->call('order:cleanup'); // command B
            }
        }
    }
}

我这样测试这个命令(简化),所以我知道主命令被激发成功了。

    $this->artisan('command')->assertExitCode(0);
    // How do I assert command A is fired?
    // How do I assert command B is fired?

我想断言子命令 1 已触发和/或子命令 2 已触发。但是如何做到这一点呢?

【问题讨论】:

    标签: laravel testing laravel-5 mocking


    【解决方案1】:

    您可以从子命令触发一个事件,然后通过Event::assertDispatched 在您的测试中断言该事件已触发

    【讨论】:

      猜你喜欢
      • 2019-03-06
      • 2020-06-13
      • 2014-01-26
      • 2017-01-07
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 2014-10-21
      • 2016-04-07
      相关资源
      最近更新 更多