【问题标题】:Undefined step reference in PhpStorm when using Codeception and Gherkin使用 Codeception 和 Gherkin 时 PhpStorm 中未定义的步骤参考
【发布时间】:2017-11-29 15:53:29
【问题描述】:

我希望在使用 Codeception 时能够在 Gherkin 功能文件中使用 PhpStorm 的“Go To Declaration”功能(Mac 上的Command + B)。但是,PhpStorm 似乎并没有弄清楚步骤的定义位置,并输出此警告:

未定义的步骤参考:[…]

当我使用 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 已安装:
  8. 确保已安装 PhpStorm 插件 GherkinCodeception Framework
  9. tests/acceptance/first.feature添加一个步骤。
  10. ./vendor/bin/codecept gherkin:snippets acceptance

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

tests/acceptance/first.feature:

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() 在哪里。我该如何解决这个问题?

【问题讨论】:

    标签: php phpstorm codeception behat gherkin


    【解决方案1】:

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

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

    它对我有用。

    【讨论】:

      【解决方案2】:

      建立Roverwolf's 答案。

      将此添加到 AcceptanceTester 文件的顶部。

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

      然后让 AcceptanceTester 实现它。像这样包装命名空间是 PHP 测试中用于伪造存在于其他命名空间中的方法的常见技巧。

      namespace {
          class AcceptanceTester extends \Codeception\Actor implements \Behat\Behat\Context\Context
          }
      }
      

      【讨论】:

        【解决方案3】:

        目前 PhpStorm 似乎使用 Behat Context 接口来确定哪些类为 .feature 文件中的 Gherkin 步骤定义了实现,因此让 PhpStorm 在 codeception 测试器中找到步骤的解决方法是添加Behat\Behat\Context\Context 接口位于源代码树中的某处

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

        然后让AcceptanceTester 实现该接口(这是一个空标记接口)

        class AcceptanceTester extends \Codeception\Actor implements Context ...
        

        【讨论】:

        • 太好了,非常感谢。我从来没有想过这一点(并且仍然不明白它为什么会起作用)。
        【解决方案4】:

        还不支持,请投票: https://youtrack.jetbrains.com/issue/WI-34963

        【讨论】:

          猜你喜欢
          • 2018-04-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-06-07
          • 1970-01-01
          相关资源
          最近更新 更多