Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behat with symfony flex

I want to use behat with symfony flex When i inject service in behat context file inside constructor, the autowire dont works. I need to explicitly configure service to public to use that service in behat. Is there another way to inject service in behat context file without making it public?

Here i am getting below error The "App\DataConsumer" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

behat.yml
   default:
     suites:
       default:
         contexts:
            - FeatureContext:
                 kernel: '@kernel'

            - ImageConsumerContext:
                 dataConsumer: '@App\DataConsumer'
like image 631
user8693894 Avatar asked Dec 14 '25 23:12

user8693894


1 Answers

UPDATE:

Starting from Symfony 4.1 it's allowed to fetch private services from tests.

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.

Source: https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing


You can make the service only public for your test environment.

In config/services_test.yaml add:

services:
  ...
  test.App\DataConsumer: '@App\DataConsumer'

Then in your behat.yml:

default:
  suites:
    default:
      contexts:
        - FeatureContext:
          kernel: '@kernel'

        - ImageConsumerContext:
          dataConsumer: '@test.App\DataConsumer'
like image 150
Fieg Avatar answered Dec 16 '25 22:12

Fieg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!