私は MSpec を使用して単体テストを作成してきましたが、BDD スタイルが本当に好きです。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 を使用して、このようなテストを作成しようとします (ただし、TestInitialize 属性を 2 つ入れているため、機能しないことがわかりますが、私がやろうとしていることはわかります..)
[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 を使用してこの方法でテストを作成するための、よりエレガントな提案を思いつく人はいますか?