我有以下设置:
Symfony 5 项目,带有 codeception 4。
我正在尝试为新创建的服务创建测试。问题是我在尝试测试时收到以下错误。
Test tests/integration/Shop/NewServiceCest.php:testSomething
Step Grab service "App\Service\NewService"
Fail Service App\Service\NewService is not available in container
服务代码如下(基本上是空的):
<?php
namespace App\Service;
class NewService
{
public function __construct()
{
}
}
测试代码如下(同样,基本上是空的):
<?php
namespace App\Tests\Integration\New;
use App\Exception\NewException;
use App\Service\NewService;
use App\Tests\IntegrationTester;
class NewServiceCest {
private NewService $newService;
public function _before(IntegrationTester $i) {
$this->newService = $i->grabService(NewService::class);
}
public function testSomething(IntegrationTester $i) {
// $i->expectThrowable(NewException::class, $this->newService->doStuff('invalidArgument'));
}
}
集成测试 yml:
actor: IntegrationTester
modules:
enabled:
- Symfony:
app_path: 'src'
environment: 'test'
- Doctrine2:
depends: Symfony
cleanup: true
dump: 'tests/_data/integration.sql'
populate: true
reconnect: true
- \App\Tests\Helper\Integration
- Asserts
我试图清除缓存,重新创建它等等。我能从 cli 中想到的一切,包括(当前路径是项目目录):
$./bin/console c:c
$./bin/console c:c --env=test
$rm -rf var (this should do nothing but still tried it)
$rm -rf tests/_support/_generated
$./bin/console cache:warmup --env=test
$./vendor/bin/codecept build
当我运行测试时,同样的问题。
使它起作用的是将服务注入某处(例如在控制器中),执行控制器,然后使用 ./vendor/bin/codecept 构建。在我完成这些步骤之后,测试按预期工作。一个明显的问题是我正在尝试创建一个用于部署的管道,在该管道中我提出了一个测试环境(干净,在部署到服务器之前即时运行),运行测试并确保一切正常。将所有服务注入某个地方只是为了能够构建不是一种选择(但我可以接受任何 cli 选项,包括运行大多数命令)。
更奇怪的是,在我这样做之后,即使我清理了缓存(var 和 _generated),现在测试也能按预期工作。就像它会将某些东西保存在我似乎找不到的不同地方(找不到任何相关的配置,而 google 和 bing 让我失望了)。
那么......为什么会发生这种情况,我怎样才能可靠地让它工作?