【发布时间】:2020-03-25 06:53:31
【问题描述】:
我为 Laravel 应用程序编写代码接收和模块 Laravel5、REST 的测试。
api测试之一:
public function testEmailRegistration(ApiTester $I) {
...
// Not correct data
$I->sendPOST($route, [
'first_name' => (string)$this->faker->randomNumber(),
'password' => $this->faker->password(1, 7),
'email' => 'not_valid_email',
]);
$I->seeResponseCodeIs(HttpCode::UNPROCESSABLE_ENTITY);
// Correct data
\Illuminate\Support\Facades\Queue::fake();
$I->sendPOST($route, [
'first_name' => $firstName,
'password' => $password,
'email' => $email,
]);
\Illuminate\Support\Facades\Queue::assertPushed(\App\Jobs\SendEmail::class);
...
}
我发送关于不正确和正确数据的请求并做出一些断言。此外,我检查了该作业是否存在于队列中。
执行测试后报错:
[Error] Call to undefined method Illuminate\Queue\SyncQueue::assertPushed()
Queue:fake之后的门面\Illuminate\Support\Facades\Queue必须解析为QueueFake,但实际上还是QueueManager,因此assertPushed函数是未定义的。
执行$I->sendPOST() 重置调用Queue::fake。它发生在 laravel 5 模块\Codeception\Lib\Connector\Laravel5,方法doRequest。
protected function doRequest($request)
{
if (!$this->firstRequest) {
$this->initialize($request);
}
$this->firstRequest = false;
$this->applyBindings();
$this->applyContextualBindings();
$this->applyInstances();
$this->applyApplicationHandlers();
$request = Request::createFromBase($request);
$response = $this->kernel->handle($request);
$this->app->make('Illuminate\Contracts\Http\Kernel')->terminate($request, $response);
return $response;
}
每次调用doRequest,除了第一个init应用程序和Queue::fake的一些配置被清除。
其中一个决定是每次测试一个请求。在测试中提出多个请求时,Queue::fake 是否还有其他变体可以工作?
【问题讨论】:
-
如果您知道如何解决,请向github.com/Codeception/module-laravel5提出拉取请求
标签: php laravel codeception