小黄瓜功能无法将任何方法与步骤匹配

Gherkin feature Cannot match any method with step

我是BDD测试

Feature: Register
    I want to register for Authenticator
    Using my name and email 

Scenario: Register for Authenticator
    Given I enter "Joe" "I" and "Doe" name, "joe.doe@ngc.com", "Password123$$$" and true to Terms of Use
    When I press register button
    Then I redirected to confirmation page

并且我在 xunit 中进行了单元测试:

[Given(@"I enter ""(\w+)"" ""(\w+)"" and ""(\w+)"" name, ""(\w+)"", ""(\w+)"" and (.*) to Terms of Use")]
        public void I_enter_registration_information(string first, string middle, string last, string email, string password, bool agree)
        {
        }

当我 运行 我的测试时,我得到这个错误:

System.InvalidOperationException : Cannot match any method with step Given I enter "Joe" "I" and "Doe" name, "joe.doe@ngc.com", "Password123$$$" and true to Terms of Use. Scenario Register for Authenticator

我尝试了与此 documentation

不同的正则表达式组合

我正在使用这个库:Xunit.Gherkin.Quick

我做错了什么?

从我的角度来看,你的代码和纯文本都很好。

但是,它看起来好像将您的一个参数变成了一个列表,而不是解析完整的字符串。我怀疑这与逗号有关。 (快速检查一下是否没有逗号。)

尝试使用非贪婪捕获:

""(\w+?)""

我找不到任何建议 Gherkin 应该以这种方式解析逗号的文档,因此这可能是该库的错误。

免责声明:我是 Xunit.Gherkin.Quick.

的作者

您的正则表达式与输入不匹配。

输入:I enter "Joe" "I" and "Doe" name, "joe.doe@ngc.com", "Password123$$$" and true to Terms of Use

正则表达式:I enter "(\w+)" "(\w+)" and "(\w+)" name, "(\w+)", "(\w+)" and (.*) to Terms of Use(我用单引号替换了双引号,因为正则表达式前面的转义字符串运算符 @ 导致双引号)。

无法将电子邮件地址 joe.doe@ngc.com 与正则表达式 (\w+) 匹配。将 Password123$$$ 与正则表达式 (\w+) 匹配也会出现同样的问题。您需要使用匹配整个输入的正确正则表达式。

例如,您可以像这样修改您的正则表达式以匹配:I enter "(\w+)" "(\w+)" and "(\w+)" name, "(.+)", "(.+)" and (.*) to Terms of Use。现在,如果您要将它放入您的属性中,请将单引号替换为双引号:

    [Given(@"I enter ""(\w+)"" ""(\w+)"" and ""(\w+)"" name, ""(.+)"", ""(.+)"" and (.*) to Terms of Use")]
    public void I_enter_registration_information(string first, string middle, string last, string email, string password, bool agree)
    {
    }

我测试过,修复后它工作正常。唯一的技巧是遵循正则表达式规则。