代码之家  ›  专栏  ›  技术社区  ›  Yngve Høiseth

使用Codeception和Gherkin时PhpStorm中未定义的步骤引用

  •  4
  • Yngve Høiseth  · 技术社区  · 7 年前

    未定义的步骤参考:[] Warning screenshot

    当我使用Behat时,PhpStorm了解步骤的定义位置。

    复制步骤

    1. mkdir codeception
    2. cd codeception
    3. composer require "codeception/codeception" --dev
    4. ./vendor/bin/codecept bootstrap
    5. ./vendor/bin/codecept generate:feature acceptance first
    6. 在PhpStorm中打开项目目录。
    7. 确保PhpStorm知道已安装Codeception: Codeception PhpStorm configuration
    8. 小黄瓜 Codeception框架
    9. tests/acceptance/first.feature .
    10. ./vendor/bin/codecept gherkin:snippets acceptance

    这将产生以下代码。(并非所有内容都包含在内,如果需要添加任何内容,请告诉我。)

    测试/验收/第一特性 :

    Feature: first
      In order to ...
      As a ...
      I need to ...
    
      Scenario: try first
        When I visit "/"
    

    tests/_support/AcceptanceTester.php

    <?php
    
    /**
     * Inherited Methods
     * @method void wantToTest($text)
     * @method void wantTo($text)
     * @method void execute($callable)
     * @method void expectTo($prediction)
     * @method void expect($prediction)
     * @method void amGoingTo($argumentation)
     * @method void am($role)
     * @method void lookForwardTo($achieveValue)
     * @method void comment($description)
     * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL)
     *
     * @SuppressWarnings(PHPMD)
    */
    class AcceptanceTester extends \Codeception\Actor
    {
        use _generated\AcceptanceTesterActions;
    
       /**
        * Define custom actions here
        */
    
        /**
         * @When I visit :arg1
         */
        public function iVisit($arg1)
        {
            throw new \Codeception\Exception\Incomplete("Step `I visit :arg1` is not defined");
        }
    }
    

    然而,PhpStorm不知道在哪里 iVisit() 是的。我该怎么解决这个问题?

    4 回复  |  直到 7 年前
        1
  •  5
  •   roverwolf    7 年前

    目前,PhpStorm似乎使用Behat上下文接口来确定中哪些类定义了小黄瓜步骤的实现 因此,让PhpStorm在codeception tester中查找步骤的一种解决方法是添加 Behat\Behat\Context\Context 接口在源代码树中的某个位置

    /* Context.php */
    namespace Behat\Behat\Context;
    
    interface Context { }
    

    AcceptanceTester 实现该接口(它是一个空的标记接口)

    class AcceptanceTester extends \Codeception\Actor implements Context ...
    
        3
  •  2
  •   Michael Ryan Soileau    7 年前

    正在构建 Roverwolf's

    namespace Behat\Behat\Context { 
       interface Context { } 
    }
    

    然后让AcceptanceTester实现这一点。在PHP测试中,像这样包装名称空间是一种常见的伎俩,可以伪造其他名称空间中存在的方法。

    namespace {
        class AcceptanceTester extends \Codeception\Actor implements \Behat\Behat\Context\Context
        }
    }
    
        4
  •  0
  •   Letter    6 年前

    我有另一个步骤定义文件,我使用Behat的上下文。然后在类声明中实现上下文。

    use Behat\Behat\Context;
    
    class myClassSteps implements Context\Context
    {
         step definitions
    }