Php 队列::fake不适用于Laravel5 codeception模块
我用codeception和模块为Laravel应用程序编写测试 api测试之一:Php 队列::fake不适用于Laravel5 codeception模块,php,laravel,codeception,Php,Laravel,Codeception,我用codeception和模块为Laravel应用程序编写测试 api测试之一: public function testEmailRegistration(ApiTester $I) { ... // Not correct data $I->sendPOST($route, [ 'first_name' => (string)$this->faker->randomNumber(), 'password' => $this->fake
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
facade\illumb\Support\Facades\Queue
之后,必须解析为QueueFake
,但实际上仍然是QueueManager
,因此assertPushed
函数未定义
执行$I->sendPOST()
重置调用Queue::false
。它发生在Laravel5模块\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
都会被清除
决定之一是每个测试一个请求。在测试中发出多个请求时,是否还有另一种变体可以工作?我不确定Laravel模块为什么会这样做,但我找到了一种解决方法,允许您使用伪造:
公共函数someTest(ApiTester$I):无效
{
//SomeFacade::fake方法调用所做的基本上是创建
//一个伪对象,并将其交换为中的原始实现
//应用程序容器,所以我们在这里重新创建该行为
//即使在发出请求后,也只会保留此内容:
$notification_fake=newnotificationfake();
//'haveInstance'是来自Laravel Codeception模块的方法
//它在应用程序容器中为您设置一个对象:
$I->haveInstance(ChannelManager::class,$notification\u-fake);
//提出请求
$I->sendPUT('someurl',$someu负载);
//断言
$I->canseeressecodeis(响应::HTTP\u OK);
$notification\u fake->assertSentToTimes($expected\u user,MyNotification::class,1);
}
请注意,该测试方法仅用于说明目的,它遗漏了许多细节,因此未定义变量等
还要注意的是,我使用的是在
illighted\Notifications\ChannelManager
注册的通知假,这与大多数可以在其别名下注册的假不同,例如queue
。因此,您必须检查实例化了什么,以及如何自己交换它。您可以在每个服务的相应服务提供商中找到此选项大多数情况下,它是立面的小写名称。如果您能找到修复方法,请向