【问题标题】:Symfony4/Behat inject function not working "you have requested a non-existen service"Symfony4/Behat 注入功能不起作用“您请求的服务不存在”
【发布时间】:2020-11-14 09:06:21
【问题描述】:

对于测试用例,我需要能够使用现有服务,但该服务不能在我的行为上下文中使用:

上下文:

/**
class CustomContext extends MinkContext implements KernelAwareContext {

    # ...

     * @param EntityManagerInterface $em
     * @param HttpCallResultPool $httpCallResultPool
     * @param SessionInterface $session
     * @param CustomService $customService
     * @param string $evaluationMode
     */
    public function __construct(
        EntityManagerInterface $em,
        HttpCallResultPool $httpCallResultPool,
        SessionInterface $session,
        CustomService $customService,
        string $evaluationMode = 'javascript'
    ) {
        $this->em = $em;
        $this->client = new Client();
        $this->inspector = new JsonInspector($evaluationMode);
        $this->httpCallResultPool = $httpCallResultPool;
        $this->session = $session;
        $this->customService= $customService;
    }

behat.yaml:

# ...
    Behat\Symfony2Extension:
      kernel:
        bootstrap: 'config/bootstrap.php' 
        path: 'src/Kernel.php'  
        class: 'App\Kernel' 
        env: dev 
        debug: false 

# ...
suites:
    default:
      contexts:
        - App\CustomContext:
            em: '@doctrine.orm.default_entity_manager'
            session: '@session'
            customService: '@App\Service\CustomService'

Fehlermeldung:

In Container.php line 289:
                                                                                                                             
  You have requested a non-existent service "App\Service\CustomService".

有人可以帮忙或知道为什么会显示此错误消息吗? 该服务确实在使用的控制器中工作。所以服务本身应该没有错误,只有注入这个服务。

【问题讨论】:

标签: symfony4 behat


【解决方案1】:

您可以按照此处的说明注入容器并访问私有服务:https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing

【讨论】:

    【解决方案2】:

    您的服务必须是公开的。您可以在常规 services.yml 中将其“全局”公开,也可以为测试环境创建覆盖。

    值得注意的是,还有另一个Symfony extension for Behat 直接使用您应用程序的服务容器并支持自动装配。不再需要将您的服务手动连接到您的上下文类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 2023-03-18
      • 1970-01-01
      • 2013-02-03
      • 2018-03-02
      • 1970-01-01
      相关资源
      最近更新 更多