简体   繁体   English

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

[英]Undefined step reference in PhpStorm when using Codeception and Gherkin

I'd like to be able to use PhpStorm's "Go To Declaration" feature ( Command + B on a Mac) in Gherkin feature files when using Codeception. 我希望能够在使用Codeception时在Gherkin功能文件中使用PhpStorm的“Go To Declaration”功能(Mac上的Command + B )。 However, PhpStorm doesn't seem to figure out where the steps are defined, and outputs this warning: 但是,PhpStorm似乎没有弄清楚步骤的定义,并输出此警告:

Undefined step reference: […] 未定义的步骤参考:[...] 警告截图

When I'm using Behat, PhpStorm understands where the steps are defined. 当我使用Behat时,PhpStorm了解步骤的定义。

Steps to reproduce 重现步骤

  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. Open the project directory in PhpStorm. 在PhpStorm中打开项目目录。
  7. Make sure that PhpStorm knows that Codeception is installed: 确保PhpStorm知道已安装Codeception: Codeception PhpStorm配置
  8. Make sure that the PhpStorm plugins Gherkin and Codeception Framework are installed. 确保安装了PhpStorm插件GherkinCodeception Framework
  9. Add a step to tests/acceptance/first.feature . 添加tests/acceptance/first.feature的步骤。
  10. ./vendor/bin/codecept gherkin:snippets acceptance

This results in the following code. 这导致以下代码。 (Not everything is included – let me know if I need to add anything.) (并非所有内容都包括在内 - 如果我需要添加任何内容,请告诉我。)

tests/acceptance/first.feature : tests/acceptance/first.feature

Feature: first
  In order to ...
  As a ...
  I need to ...

  Scenario: try first
    When I visit "/"

tests/_support/AcceptanceTester.php : 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");
    }
}

However, PhpStorm doesn't know where iVisit() is. 但是,PhpStorm不知道iVisit()位置。 How can I fix this? 我怎样才能解决这个问题?

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

Currently PhpStorm seems to use the Behat Context interface to determine which classes define implementations for the Gherkin steps in .feature files, so a workaround to have PhpStorm find the steps in the codeception tester is to add the Behat\\Behat\\Context\\Context interface somewhere in your source tree 目前,PhpStorm似乎使用Be​​hat Context接口来确定哪些类为.feature文件中的Gherkin步骤定义实现,因此让PhpStorm找到代码测试测试程序中的步骤的一种解决方法是在某处添加Behat Behat\\Behat\\Context\\Context接口在你的源代码树中

/* Context.php */
namespace Behat\Behat\Context;

interface Context { }

and then have the AcceptanceTester implement that interface (which is an empty marker interface) 然后让AcceptanceTester实现该接口(这是一个空标记接口)

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

Building off Roverwolf's answer. 建立Roverwolf的答案。

Add this to the top of the AcceptanceTester file. 将其添加到AcceptanceTester文件的顶部。

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

Then have AcceptanceTester implement that. 然后让AcceptanceTester实现它。 Wrapping namespaces like this is a common trick in PHP testing to fake methods that exist in other namespaces. 像这样包装命名空间是PHP测试中常见的技巧,可以伪造其他命名空间中存在的方法。

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

I have another step definition file, and I use Behat's Context. 我有另一个步骤定义文件,我使用Behat的Context。 Then implements Context in the class declaration. 然后在类声明中实现Context。

use Behat\Behat\Context;

class myClassSteps implements Context\Context
{
     step definitions
}

It works for me. 这个对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Codeception Gherkin,定义步骤实现路径 - Codeception Gherkin, defining the step implementation path 如何使用PhpStorm中的Codeception进入我的代码(而不是测试代码)? - How do I step into my code (not the test code) using Codeception in PhpStorm? 在多个代码功能测试中使用相同的小黄瓜步骤 - Using the same gherkin steps in multiple codeception functional tests 功能测试失败时如何停止 Github 操作步骤(使用 Codeception) - How to stop Github Actions step when functional tests failed (using Codeception) 小黄瓜-在许多情况下(代码接收)包括通用步骤 - Gherkin - Include common steps in many scenarios (Codeception) Codeception,小黄瓜测试,如何组织我的代码? - Codeception, gherkin test, how organize my code? PhpStorm,Yii2基础和代码接收 - PhpStorm, Yii2 Basic and Codeception 尝试运行单元测试时的代码接收“未定义的变量输出” - Codeception 'Undefined variable output' when trying to run a unit test 在代码接收中使用StepObjects时,错误消息:[ErrorException]未定义的变量:方案 - Using StepObjects in codeception, error message: [ErrorException] Undefined variable: scenario 所有Codeception Gherkin .feature测试是否都在Context文件中结束吗? - Do all Codeception gherkin .feature tests end up in Context files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM