避免在小黄瓜中重复自己

Mar*_*rst 6 cucumber specflow gherkin

所以我一直在使用specflow一段时间,而且一直在困扰我.

这是我们当前使用的场景的示例:

Scenario: Select a region via selection button
    When I select "Scotland" from Available Regions
    And I click on the "Select One" button
    Then Available Regions does not contain "Scotland"
    And Selected Regions contains "Scotland"
Run Code Online (Sandbox Code Playgroud)

是否有一个避免在几乎每一行都说"苏格兰"?或者它是否使它更具可读性,我应该坚持下去?

AlS*_*Ski 7

在我尝试回答你的问题之前,我是否可以建议你花一点时间去阅读Dan North's Who的域名无论如何.

首先,我想摆脱所说的那条线And I click on the "Select One" button,因为我认为这应该是隐含的一部分When I select "Scotland" from Available regions.

现在你有了

Scenario: Select a region 
    When I select "Scotland" from Available Regions
    Then Available Regions does not contain "Scotland"
    And Selected Regions contains "Scotland"
Run Code Online (Sandbox Code Playgroud)

你可以把它写成

Scenario: Select a region 
    When I select "Scotland" from Available Regions
    Then Available Regions does not contain the last selected region
    And Selected Regions contains the last selected region
Run Code Online (Sandbox Code Playgroud)

有什么区别吗?好吧可能不是.

我发现,因为我花了更多的时间在黄瓜上,因为它有助于重构你的场景,就像你重构它们背后的代码一样.在C#/ SpecFlow中我们可以实现

    Then Available Regions does not contain "Scotland"
Run Code Online (Sandbox Code Playgroud)

    [Then("Available Regions does not contain (.*)")]
    public void ThenAvailableRegionsDoesNotContain(string region)
    {
        AvailableRegions.Contains(region).ShouldBeFalse();
    }
Run Code Online (Sandbox Code Playgroud)

    Then Available Regions does not contain the last selected region
Run Code Online (Sandbox Code Playgroud)

    [Then("Available Regions does not contain the last selected region")]
    public void ThenAvailableRegionsDoesNotContainLastSelectedRegion()
    {
        ThenAvailableRegionsDoesNotContain(LastSelectedRegion);
    }
Run Code Online (Sandbox Code Playgroud)

老实说,这取决于你.你更喜欢哪种方言