3

我最近强烈采用了 BDD 设计以及使用 MSpec 来实现 xSpecification 测试。这导致一些相当疯狂的类名变得难以区分解决方案资源管理器中的意图。举个例子:

[Subject(typeof(MuchGoodServiceOrchestrator))]
public class Given_i_am_a_logged_user_and_view_my_some_company_super_things 
    : WithSubject<MuchGoodServiceOrchestrator>

我最初的一些想法可能是使用解决方案文件夹并执行类似的操作

Given_I_am_logged_in \When_I_view_my_some_company_super_things.cs

这将使我有可能进一步深入

Given_I_am_logged_in \ And_things_are_good \When_I_view_my_some_company_super_things.cs Given_I_am_logged_in \ And_things_are_bad \When_I_view_my_some_company_super_things.cs

有没有人成功地做类似的事情,或者你在命名 xSpecification 测试时发现了什么?

4

2 回答 2

3

警告:我不是这方面的专家,我孤立地工作,但这是我个人基于几个月的反复试验得出的结论。

主题属性超载[Subject(Type subjectType, string subject)]

也许您可以使用 string 参数来记录您的担忧,因此您可能会使用以下内容:

[Subject(typeof(MuchGoodServiceOrchestrator), "Logged in"]

-和-

[Subject(typeof(MuchGoodServiceOrchestrator), "Not logged in"]

如果您使用的是约定,请稍微扩展一下

  • 鉴于系统处于此特定状态
  • 这件有趣的事情发生时
  • 那么这些就是后果

这是表达Arrange, Act, Assert模式的另一种方式。Given ( Arrange ) 是您的上下文,是测试的先决条件。When ( Act ) 是您正在测试的活动,Then ( Assert )是您验证预期行为的地方。

在 MSpec 中,我通常使用这种模式:

public class when_doing_domething : with_context
{
  It should_behave_like_this;  // One or more assertions.
  It should_also_behave_like_this;
}

因此,要尝试使用您的问题域中的术语:

public class with_logged_in_user
{
  protected static User User;
  protected static MuchGoodServiceOrchestrator sut;
  // Arrange
  Establish context =()=> 
  {
    User = new User() { /* initialize the User object as a logged in user */ };
    sut = new MuchGoodServiceOrchestrator(User); // Dependency injection
  };
}

public class with_anonymous_user
{
  protected static User User;
  protected static MuchGoodServiceOrchestrator sut;
  // Arrange
  Establish context =()=> 
  {
    User = new User() { /* initialize the User object as anonymous or not logged in */ };
    sut = new MuchGoodServiceOrchestrator(User); // Dependency injection
  };
}

[Subject(typeof(MuchGoodServiceOrchestrator), "Logged in")]
public class when_viewing_things_as_a_logged_in_user : with_logged_in_user
{
  // Act
  Because of =()=> sut.CallTheCodeToBeTested();
  // Assert
  It should_do_this =()=> sut.Member.ShouldEqual(expectedValue); // Assert
}

[Subject(typeof(MuchGoodServiceOrchestrator), "Not logged in")]
public class when_viewing_things_while_not_logged_in : with_anonymous_user
{
  // Etc...
}
于 2011-12-09T17:31:03.507 回答
1

将给定对象的所有测试类分组到一个文件下,例如 SuperThingTests 将包含围绕 SuperThing 类的所有测试。

于 2011-12-08T17:34:44.857 回答