我有以下設置:Symfony 5 項目,帶有 codeception 4。我正在嘗試為新創建的服務創建測試。問題是我在嘗試測試時收到以下錯誤。Test tests/integration/Shop/NewServiceCest.php:testSomethingStep Grab service "App\Service\NewService"Fail Service App\Service\NewService is not available in container服務代碼如下(基本上是空的):<?phpnamespace App\Service;class NewService{ public function __construct() { }}測試代碼如下(同樣,基本上是空的):<?phpnamespace 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: IntegrationTestermodules: 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 讓我失望了)。那么......為什么會發生這種情況,我怎樣才能可靠地讓它工作?
2 回答

明月笑刀無情
TA貢獻1828條經驗 獲得超4個贊
問題是,Symfony 將從編譯的容器中刪除未使用的服務。我推薦這個問題以獲取更多信息:https ://github.com/symfony/symfony-docs/issues/9890

泛舟湖上清波郎朗
TA貢獻1818條經驗 獲得超3個贊
觀察到相同的行為。$I->grabService(NewService::class)
只要您不在代碼中的其他地方使用此服務(例如注入某些控制器等),新創建的服務在 codeception 中不可用。
- 2 回答
- 0 關注
- 135 瀏覽
添加回答
舉報
0/150
提交
取消