FriendsOfBehat / SymfonyExtension

:musical_score: Extension integrating Behat with Symfony.
MIT License
472 stars 62 forks source link

Unable to run after instalation #146

Open AntonioCS opened 3 years ago

AntonioCS commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

AntonioCS commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@udan this was test and it didn't work

dkarlovi commented 3 years ago

@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 commented 3 years ago

@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 commented 2 years ago

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

Tealsky commented 1 year ago

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 commented 1 week ago

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