【问题标题】:My Behat Scenario Outline's step definition returns undefinedMy Behat Scenario Outline 的步骤定义返回 undefined
【发布时间】:2015-10-07 23:02:54
【问题描述】:

我正在使用 Behat v3.0 为我的代码创建测试,但我的功能文件的场景大纲中有几个步骤仍未定义。

我的场景大纲示例:

Scenario Outline: Send email validation messages
    Given I have the email <email>
    When I send the email
    Then the response status code should be "500"
    And I get a message from the <variable> variable that reads <message>

    Examples:
        | email | variable | message                                  |
        |       | email    | The email field is required.             |
        | -1    | email    | The email must be a valid email address. |
        | email | email    | The email must be a valid email address. |

我使用了http://docs.behat.org/en/stable/guides/1.gherkin.html#scenario-outlines 的文档。

为了定义步骤,我在 FeatureContext.php 中为两个问题步骤提供了步骤定义方法:“我有电子邮件 ”和“我从读取 的变量中收到一条消息”:

/**
 * @Given I have the email :email
 */
public function iHaveTheEmail($email)
{
    // Save the email address
}

/**
 * @Then I get a message from the :variable variable that reads :message
 */
public function iGetAMessageFromTheVariableThatReads($variable, $message)
{
    // Check the response for the validation errors
}

我使用了http://docs.behat.org/en/v3.0/guides/2.definitions.html的文档

运行 Behat 的输出坚持认为这些步骤仍未定义。只有带有与示例列表关联的参数的步骤存在此问题。步骤“响应状态代码应为'500'”按预期工作。

我找不到在场景大纲中定义步骤的具体示例,而且 Behat 的文档通常很少。

我可以像 Behat v2.5 那样使用正则表达式注释来定义步骤,但这是唯一的解决方案吗?

【问题讨论】:

    标签: php bdd behat gherkin


    【解决方案1】:

    你应该为你的论点使用引号:

    Scenario Outline: Send email validation messages
        Given I have the email "<email>"
        When I send the email
        Then the response status code should be "500"
        And I get a message from the "<variable>" variable that reads "<message>"
    

    或者你可以使用正则表达式,就像 Mink Extension 那样:

    https://github.com/Behat/MinkExtension/blob/master/src/Behat/MinkExtension/Context/MinkContext.php#L38

    【讨论】:

    • 修复了变量和消息步骤,但电子邮件步骤在空行上中断,其中消息列显示“电子邮件字段是必需的”。有没有表示空值或空值的好方法?
    • 我决定将值“[null]”用于空值。参数将作为字符串传递给定义的 step 方法,如果字符串为“[null]”,则将其分配给 null 而不是字符串本身。这解决了问题。谢谢你的帮助,b263。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多