如何命名 xSpecification/BDD 测试类以便它们传达意图?特别是在解决方案资源管理器中

发布于 2024-12-20 09:32:47 字数 732 浏览 2 评论 0原文

我最近强烈采用了 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 测试中发现了什么成功?

I've recently adopted strongly following BDD design along with usage of MSpec for implementing xSpecification tests. This has been leading to some rather insane class names that become hard to distinguish intent inside the solution explorer. Take for example:

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

Some of my initial thoughts have been perhaps to use solution folders and do something like

Given_I_am_logged_in \ When_I_view_my_some_company_super_things.cs

This would allow me to potentially drill down further

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

Has anyone had success doing something similar, or what have you found success with in naming xSpecification tests?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心旳酸楚 2024-12-27 09:32:47

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

subject 属性 [Subject(Type subjectType, string subject)]

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

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

-and-

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

对此进行一些扩展,如果你正在使用约定

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

这是表达<的另一种方式em>安排、行动、断言模式。 给定安排)是您的上下文,是测试的先决条件。 何时 (行动) 是您正在测试的活动,然后< /em> (断言) 是您验证预期行为的地方。

在 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...
}

Caveat: I'm not an expert at this and I work in isolation, but this is my personal take based on several months of trial and error.

There's an overload of the subject attribute [Subject(Type subjectType, string subject)]

Perhaps you could use the string parameter to document your concern, so perhaps you'd use something like:

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

-and-

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

Expanding on that a bit, if you are using the convention

  • Given the system is in this particular state
  • When this interesting thing happens
  • Then these are the consequences

That is another way of expressing the Arrange, Act, Assert pattern. The Given (Arrange) is your context, the preconditions for the test. The When (Act) is the activity you're testing and the Then (Assert) is where you verify expected behaviour.

in MSpec, I usually use this pattern:

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

So to try to use terms from your problem domain:

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...
}
短暂陪伴 2024-12-27 09:32:47

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

Group all the test classes for a given object under one file e.g. SuperThingTests would contain all of your tests around the SuperThing class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文