绑定中的 SpecFlow 歧义

SpecFlow Ambiguity in bindings

我使用 Spec-flow 已经有好几天了。我面临 "Multiple matching found.Navigating to first match",虽然调试可以解决这个问题,但是当我 运行 时,整个解决方案由于绑定中的歧义而失败。我在单个项目中 运行 大约 4 C 升 class 个文件

Feature: ConversionUnencrypted Pdf-Pdf

@mytag
Scenario Outline: ConversionUnencrypted Pdf-Pdf

    Given I get Inputs Folder and list of Files <inputFolder> then <getInputTokens>(Multiple bindings for this line)
    Given I get <outputDirectory>
    Given I set saving  Mode <ConversionMode>
    Given I convert pdf using Conversion
    Given I convert to Image <convertToFile>
    Then I compare Images <getActualImagePath> and <getExpectedImagePath> and <pageCount>

和步骤定义:

**Binding for Multiple steps is the below binding:**

第一次绑定:

[Given(@"I get Inputs Folder and list of Files (.*) then (.*)")]
public void GivenIGetInputsFolderAndListOfFilesThen(string getInputFolder, string getInputTokens)        
{
       --logic--
}

第二次绑定:

[Given(@"I get (.*)")]
public void GivenIGet(string getOutputDirectory)
{
      --logic--
}

第二个绑定修改为:

Given I set OutputDirectory of Pdf <outputDirectory>

[Given]
public void Given_I_set_OutputDirectory_of_Pdf_P0(string p0)
{
  --logic--
}

这个也没有帮助我。也尝试过 Regex 仍然无法解决问题。 上述 2 个绑定存在歧义。它不仅在一个功能中,它也观察到其他功能文件。 如何解决这个问题,使每一行都与一个 Binding 完全匹配?

您需要使用Scoped Bindings,以便可以对具有相同描述的场景和步骤进行分区。请在此处查看演练:

http://www.specflow.org/documentation/Scoped-bindings/

或此处:

https://github.com/techtalk/SpecFlow/wiki/Scoped-bindings

我想我能猜到那条遗漏的信息。

问题出在这两行的绑定中:

Given I get Inputs Folder and list of Files <inputFolder> then <getInputTokens>
Given I get <outputDirectory>

您给出的第一个绑定:

[Given(@"I get Inputs Folder and list of Files (.*) then (.*)")]
public void GivenIGetInputsFolderAndListOfFilesThen(string getInputFolder, string getInputTokens)        
{
    // etc
}

我猜是第二个绑定

[Given(@"I get (.*)")]
public void GivenIGet(string outputDirectory)        
{
    // etc
}

第二个绑定匹配任何以 "Given I get" 开头的文本,当然包括任何以 "Given I get Inputs Folder and list of Files ..."

开头的文本

如果您想避免绑定冲突,您需要更具体地使用 "given I get " 绑定。执行此操作时,您应该选择业务专家(而非代码专家)能够理解您的步骤的语言 - "I get " 可能不是业务专家会说的。但它可能是,在技术含量高的组织中 - 在这种情况下,您需要使正则表达式匹配 filepath/uri 更具体。

不了解 Regex 就无法有效地使用 Specflow。 (.*) 是最宽松的捕获组,即使 specflow 插件默认给出它,也很少是一个好的答案。要获得您可以使用的字符的快速列表,并测试正则表达式的想法,为什么不看看 rubular.com.

正如@perfectionist 所指出的,您的问题出在您的正则表达式上。一个正在消耗两者的所有字符。试试这个:

Feature: ConversionUnencrypted Pdf-Pdf

@mytag
Scenario Outline: ConversionUnencrypted Pdf-Pdf

    Given I get Inputs Folder and list of Files '<inputFolder>' then '<getInputTokens>'
    Given I get '<outputDirectory>'
    ...

和步骤定义:

[Given(@"I get Inputs Folder and list of Files '([^']*)' then '([^']*)'")]
public void GivenIGetInputsFolderAndListOfFilesThen(string getInputFolder, string getInputTokens)        
{
}

[Given(@"I get '([^']*)'")]
public void GivenIGet(string getOutputDirectory)
{
      --logic--
}

只有当输入不包含 ' 字符时,此正则表达式才会匹配,因此会防止第二种方法在使用输入和匹配较长方法时过于贪婪。

作为一般规则,我更喜欢在场景中像上面那样将字符串字符用单引号引起来,因为这样可以稍微轻松地缓解此类问题

显然,只有当您的输入 '<inputFolder>','<getInputTokens>' and <outputFolder> 不包含任何 ' 字符时,以上内容才有效。如果他们这样做,那么您可能需要更复杂的正则表达式