【发布时间】:2015-03-27 04:16:01
【问题描述】:
我在 Laravel 5 事件处理程序中有一行代码,如下所示:
$this->event->batch->increment('attempted_jobs');
$this->event 是调用处理程序的事件,$this->event->batch 包含我的Batch 模型。所有这些都是在我的数据库中增加attempted_jobs 列,所以这是相当基本的东西。
我希望能够测试这个事件处理程序,我正在使用 Codeception 和 Mockery。我对$this->event->batch 的模拟如下所示:
$batch = m::mock('MyVendor\MyApp\Batch');
$batch->shouldReceive('increment')->once()->with('attempted_jobs');
但这会导致问题 - increment() 是 Model 的受保护方法,因此无法模拟。这是确切的错误:
InvalidArgumentException: increment() cannot be mocked as it a protected method and mocking protected methods is not allowed for this mock
它似乎是使用__call() PHP 魔术方法实现的,那么我该如何模拟呢?我尝试创建一个 __call() 模拟,但这会产生大量与未实现的 increment() 方法相关的错误。
【问题讨论】:
标签: php laravel phpunit codeception mockery