我想将 behat 与 symfony flex 一起使用 当我在构造函数内的行为上下文文件中注入(inject)服务时, Autowiring 不起作用。 我需要显式地将服务配置为公共(public)才能在行为中使用该服务。 是否有另一种方法可以在 behat 上下文文件中注入(inject)服务而不将其公开?
这里我遇到以下错误 编译容器时,“App\DataConsumer”服务或别名已被删除或内联。您应该将其公开,或者停止直接使用容器并使用依赖项注入(inject)。
behat.yml
default:
suites:
default:
contexts:
- FeatureContext:
kernel: '@kernel'
- ImageConsumerContext:
dataConsumer: '@App\DataConsumer'
最佳答案
更新:
从 Symfony 4.1 开始,允许从测试中获取私有(private)服务。
The only remaining drawback of "private services by default" is that testing was harder than before. Some developers even defined some config in the test environment to make all services public in tests. In Symfony 4.1, we did the same and now tests allow fetching private services by default.
来源:https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing
<小时/>您可以仅在您的测试环境中公开该服务。
在config/services_test.yaml
中添加:
services:
...
test.App\DataConsumer: '@App\DataConsumer'
然后在你的 behat.yml 中:
default:
suites:
default:
contexts:
- FeatureContext:
kernel: '@kernel'
- ImageConsumerContext:
dataConsumer: '@test.App\DataConsumer'
关于使用 symfony flex 进行行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47394763/