phpautomated-testsbddbehat

Behat thinks that steps are missing in the FeatureContext


Windows 10, PHP 8.4.13, Behat 3.25.0.0.

File features/basket.feature:

Feature: Product basket
  In order to buy products
  As a customer
  I need to be able to put interesting products into a basket

  Scenario: Buying a single product under 10 dollars
    Given product "A book" with price 5

File features/bootstrap/FeatureContext.php:

<?php

use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use PHPUnit\Framework\Assert;

class FeatureContext implements Context
{
    public function __construct()
    {
    }

    #[Given('product :arg1 with price :arg2')]
    public function productWithPrice($arg1, $arg2): void
    {

    }
}

Run tests:

php vendor/bin/behat

Output:

--- FeatureContext has missing steps. Define them with these snippets:

    #[Given('product :arg1 with price :arg2')]
    public function productWithPrice2($arg1, $arg2): void
    {
        throw new PendingException();
    }

Why does Behat show this message? I have already defined this step at FeatureContext.


Solution

  • Behat behaved like this, because I forgot to import the class for Given. The correct version of features/bootstrap/FeatureContext.php:

    <?php
    
    use Behat\Behat\Context\Context;
    use Behat\Gherkin\Node\PyStringNode;
    use Behat\Gherkin\Node\TableNode;
    use Behat\Behat\Tester\Exception\PendingException; // <-- Add this
    use Behat\Step\Given; // <-- Add this
    use Behat\Step\When; // <-- Add this (if you plan to use it in future tests)
    use Behat\Step\Then; // <-- Add this (if you plan to use it in future tests)
    
    class FeatureContext implements Context
    {
        public function __construct()
        {
        }
    
        #[Given('product :arg1 with price :arg2')]
        public function productWithPrice($arg1, $arg2): void
        {
            throw new PendingException();
        }
    }
    

    Now command php vendor/bin/behat returns the correct output and doesn't tell that FeatureContext has missing steps:

    Feature: Product basket
      In order to buy products
      As a customer
      I need to be able to put interesting products into a basket
    
      Scenario: Buying a single product under 10 dollars # features\basket.feature:6
        Given product "A book" with price 5              # FeatureContext::productWithPrice()
          TODO: write pending definition
    
    1 scenario (1 pending)
    1 step (1 pending)
    0m0.06s (9.15Mb)