在Specflow步骤中组织代码

Kir*_*eed 4 specflow

我从Techtalk上了解到,将步骤定义与功能耦合是一种反模式。但是,我想知道如何组织步骤定义,以便在代码中轻松看到哪些步骤组合在一起。

例如,我应该为每个功能创建一个代码文件,为共享的步骤创建一个单独的文件吗?还是应该有一套功能的代码文件?

Gre*_*rdt 5

我倾向于以几种方式来组织步骤定义,具体取决于步骤定义文件的大小。

分开的数据和UI步骤

我有很多Given some thing exists in the database步骤,因此通常将它们放入一个名为DataSteps.cs的文件中。我也有很多Then something exists in the database步骤,这些都进入了DataAssertionSteps.cs。

我所有的When I fill in some form field步骤都在FormSteps.cs中进行。每当我需要Then something in the form is enabled|disabled或断言表单字段具有某个值时,我都会将其扔到FormPresentationSteps.cs中。

按模型分开的步骤

有时,我的步骤定义文件会变得很大,因此我开始将步骤定义移动到与某个模型相关的文件中。说我有一个Person模特。我可能会创建一个PersonSteps.cs文件,该文件分为三个区域:

[Binding]
public class PersonSteps
{
    #region Given
    // Given a person exists with the following attributes:

    #endregion

    #region When
    // When something something about a Person
    #endregion

    #region Then
    // Then a person should exist with the following attributes:
    #endregion
}
Run Code Online (Sandbox Code Playgroud)

基本上,我从以下文件开始:

  • DataSteps.cs- Given用于设置测试数据的
  • DataAssertionSteps.cs- Then用于断言数据库中正确存在数据的s
  • PresentationAssertionSteps.cs-仅用于确保UI看起来应该是常规的东西
  • FormSteps.cs-操作表单的步骤
  • FormPresentationAssertionSteps.cs-确保正确启用/禁用表单字段具有正确的值。显示验证消息,等等。
  • GeneralSteps.cs-一个名称不正确的包罗万象,适用于上述文件中未包含的内容。也许我应该将其重命名为“ AndIShallCallItSomethingManagerSteps.cs”?