我最近强烈采用了 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 测试方面发现了什么?
最佳答案
警告:我不是这方面的专家,我是独立工作的,但这是我基于几个月的反复试验得出的个人看法。
主题属性重载[Subject(Type subjectType, string subject)]
也许您可以使用字符串参数来记录您的顾虑,所以也许您会使用如下内容:
[Subject(typeof(MuchGoodServiceOrchestrator), "Logged in"]
-和-
[Subject(typeof(MuchGoodServiceOrchestrator), "Not logged in"]
如果您使用的是约定,请稍微扩展一下
- 给定系统处于这个特定状态
- 什么时候这件有趣的事情发生
- 那么这些就是后果
这是表达Arrange, Act, Assert 模式的另一种方式。 Given(Arrange)是你的上下文,是测试的前提条件。 When(Act)是您正在测试的事件,Then(断言)是您验证预期行为的地方。
在 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...
}
关于c# - 您如何命名 xSpecification/BDD 测试类以便它们传达意图?特别是在解决方案资源管理器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8435076/