【问题标题】:Laravel testing fake eventLaravel 测试假事件
【发布时间】:2021-08-12 17:12:44
【问题描述】:

我试图伪造事件

public function should_assign_order()
{
    Event::fake([OrderWasAssigned::class]);
.
.
.
}

我收到了这个错误

Call to undefined method Illuminate\Support\Testing\Fakes\EventFake::getListeners() 编辑:

我尝试将望远镜禁用为this issueputenv('TELESCOPE_ENABLED=false'); 同样的问题

我只需要伪造这个事件

我在 laravel 6

PHPUnit 9.5.4.

【问题讨论】:

    标签: laravel phpunit


    【解决方案1】:

    如果我理解正确,那么您正在寻找范围内的事件,因为您想测试特定事件

    /**
    * Test order process.
    */
        public function test_orders_can_be_processed()
        {
            $order = Event::fakeFor(function () {
                $order = Order::factory()->create();
    
                Event::assertDispatched(OrderCreated::class);
    
                return $order;
            });
    
            // Events are dispatched as normal and observers will run ...
            $order->update([...]);
        }
    }
    

    你也可以在laravels documentation about scoped events阅读它

    【讨论】:

      猜你喜欢
      • 2019-07-13
      • 2019-11-26
      • 2022-01-03
      • 1970-01-01
      • 2015-05-22
      • 2017-09-22
      • 2017-05-21
      • 2021-05-27
      • 2016-07-24
      相关资源
      最近更新 更多