Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to run after instalation #146

Open
AntonioCS opened this issue Mar 5, 2021 · 11 comments
Open

Unable to run after instalation #146

AntonioCS opened this issue Mar 5, 2021 · 11 comments

Comments

@AntonioCS
Copy link

Hey,
just followed this article here https://pavlakis.dev/posts/upgrade-to-friends-of-behat-symfony-extension/
to get behat up and running in symfony 4.
I ran the following commands:

composer require --dev friends-of-behat/symfony-extension:^2.0
composer require --dev friends-of-behat/mink friends-of-behat/mink-extension friends-of-behat/mink-browserkit-driver

I then run:

./vendor/bin/behat

and I get the following error:

In ContextServiceEnvironmentHandler.php line 195:
  Kernel "App\Kernel" used by Behat with "local" environment and debug enabled needs to have "FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle" bundle registered.  

Not sure why this isn't using the test env but I went to bundles.php and changed test to all in FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle::class => ['all' => true]
I ran the command again and got this error:

In Validator.php line 61:
  Can not find a matching value for an argument `$kernel` of the method `App\Tests\Behat\DemoContext::__construct()`.  

What am I doing wrong? I basically just installed this

@alanbem
Copy link

alanbem commented Apr 5, 2021

Same here - I was able to narrow down the culprit to \Behat\Behat\Context\ContextFactory::createContext(), but why its happening I don't know. Instead of getting context from container behat tries to instantiate it on its own.

@alanbem
Copy link

alanbem commented Apr 6, 2021

@AntonioCS is your context definiton autoconfigured - autoconfigure: true? Because mine wasn't, after changed it it all started to work.

@AntonioCS
Copy link
Author

I have this in services.yaml:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

and this in services_test.yaml:

services:
    _defaults:
        autowire: true
        autoconfigure: true

@udan
Copy link

udan commented May 25, 2021

This is your main clue and your mistake

Not sure why this isn't using the test env but I went to bundles.php and changed test to all in FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle::class => ['all' => true]

Just revert bundle registration to 'test' and run:

APP_ENV=test ./vendor/bin/behat

@AntonioCS
Copy link
Author

@udan this was test and it didn't work

@dkarlovi
Copy link

dkarlovi commented Jun 5, 2021

@AntonioCS you can do this

bin/console debug:container -e test demo

it should give you your context:

/app $ bin/console debug:container -e test demo

Information for Service "App\Tests\Behat\DemoContext"
=====================================================

 This context class contains the definitions of the steps used by the demo feature file. Learn how to get started with Behat and BDD on Behat's website.

 ---------------- ----------------------------- 
  Option           Value                        
 ---------------- ----------------------------- 
  Service ID       App\Tests\Behat\DemoContext  
  Class            App\Tests\Behat\DemoContext  
  Tags             -                            
  Public           yes                          
  Synthetic        no                           
  Lazy             no                           
  Shared           yes                          
  Abstract         no                           
  Autowired        yes                          
  Autoconfigured   yes                          
 ---------------- ----------------------------- 

If not, your services_test.yaml is misconfigured, you need this:

services:
    _defaults:
        autowire: true
        autoconfigure: true

    App\Tests\Behat\:
        resource: '../tests/Behat/*'

@dkarlovi
Copy link

@pamil I see you up voted. 😁 IMO this popping up again and again in the issues points to people not understanding how contexts are loaded.

I had the same issue and needed to debug it, found out my test services weren't being loaded (a bug in Symfony IMO), saw that contexts which cannot be loaded from the container fall back to regular Behat behaviour.

This might be too subtle and fragile, I'd make that opt in, but default to using only the container and erroring if not possible. This is very likely to remove most/all issues related to this misconfiguration.

@hulkthedev
Copy link

hulkthedev commented Oct 11, 2021

Move your Config to services.yml because services_test.yml ist never read by symfony because Behat runs in dev mode.

@Tealsky
Copy link

Tealsky commented Mar 14, 2023

Hello @AntonioCS, just encountered the same problem with Symfony 6.2.
To solve the issue, I explicitly set the test environment in the behat.yaml file.
Documented here : https://github.com/FriendsOfBehat/SymfonyExtension/blob/master/DOCUMENTATION.md#configuration-reference

default:
    suites:
        default:
            contexts:
                - App\Tests\Behat\DemoContext

    extensions:
        FriendsOfBehat\SymfonyExtension:
            kernel:
                environment: test
            bootstrap: tests/bootstrap.php

Instead of

default:
    suites:
        default:
            contexts:
                - App\Tests\Behat\DemoContext

    extensions:
        FriendsOfBehat\SymfonyExtension: ~

@regisnew
Copy link

This is your main clue and your mistake

Not sure why this isn't using the test env but I went to bundles.php and changed test to all in FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle::class => ['all' => true]

Just revert bundle registration to 'test' and run:

APP_ENV=test ./vendor/bin/behat

This works for me. Thanks

@momito69
Copy link

Thanks @regisnew , your solution did work very well ! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants