我一直在使用 MSpec 来编写我的单元测试,并且真的更喜欢 BDD 风格,我认为它更具可读性。我现在使用的是 MSpec 不支持的 Silverlight,所以我不得不使用 MSTest,但仍想保持 BDD 风格,所以我正在尝试找出一种方法来做到这一点。
只是为了解释我想要实现的目标,这就是我编写 MSpec 测试的方式
[Subject(typeof(Calculator))]
public class when_I_add_two_numbers : with_calculator
{
Establish context = () => this.Calculator = new Calculator();
Because I_add_2_and_4 = () => this.Calculator.Add(2).Add(4);
It should_display_6 = () => this.Calculator.Result.ShouldEqual(6);
}
public class with_calculator
{
protected static Calculator;
}
因此,使用 MSTest,我会尝试像这样编写测试(尽管您可以看到它不起作用,因为我已经放入了 2 个 TestInitialize 属性,但是您得到了我想要做的事情..)
[TestClass]
public class when_I_add_two_numbers : with_calculator
{
[TestInitialize]
public void GivenIHaveACalculator()
{
this.Calculator = new Calculator();
}
[TestInitialize]
public void WhenIAdd2And4()
{
this.Calculator.Add(2).Add(4);
}
[TestMethod]
public void ThenItShouldDisplay6()
{
this.Calculator.Result.ShouldEqual(6);
}
}
public class with_calculator
{
protected Calculator Calculator {get;set;}
}
任何人都可以提出一些更优雅的建议来使用 MSTest 以这种方式编写测试吗?