【问题标题】:Messenger with multiple buses多辆巴士的信使
【发布时间】:2018-11-02 10:42:37
【问题描述】:

我有多辆巴士

framework:
    messenger:
        default_bus: messenger.bus.command
        buses:
            messenger.bus.command: ~
            messenger.bus.query: ~

在我的单元测试中,我有类似的东西

$this->commandBus = static::$container->get('messenger.bus.command');

我明白了

Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: “messenger.bus.command”服务或别名已被删除或 编译容器时内联。你应该要么做到 public,或者直接停止使用容器并使用依赖 而是注射。

如何解决?谢谢。

【问题讨论】:

    标签: symfony


    【解决方案1】:

    Symfony 4.1 添加了“test container”,它允许访问原本属于私有服务的内容。但是,您还需要明确地make the service public(至少在您的测试环境中),所以它不会被删除。

    # config/services.yaml
    services:
        test.messenger.bus.command:
            alias: messenger.bus.command
            public: true
    
    class AddUserCommandTest extends WebTestCase
    {
        private function assertUserCreated()
        {
            self::bootKernel();
    
            // gets the special container that allows fetching private services
            $container = self::$container;
    
            $this->assertTrue(self::$container->get('security.password_encoder')->isPasswordValid($user, '...');
            // ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-15
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多