bit*_*onk 3 tdd bdd comparison nunit mspec
我正在尝试开始使用纯TDD并考虑BDD风格.我试图掌握,与MSpec相比,写作单元测试的感觉与NUnit完全不同.
考虑一个简单的MSpec测试,如下所示:
[Subject("PersonRepository")]
class when_adding_a_new_person
{
Establish context = () => sut = new PersonRepository();
Because of = () => sut.AddPerson("Jim", "Panse");
It should_have_a_person = sut.Count.ShouldEqual(1);
It should_have_that_persion = sut.Contains("Jim", "Panse");
static PersonRepository;
}
Run Code Online (Sandbox Code Playgroud)
你如何以干净的方式将其转换为NUnit,但不使用任何BDD扩展或任何东西.我认为每个应该断言是一个可单独运行的测试Establish并且 Because应该只对所有断言执行一次是个好主意.我可以使用[Setup]的Establish和 Because,但会为每个测试运行.我可以用Assert.的It,但是这不会让他们单独运行的测试.
这个例子与NUnit风格相比如何?
I generally would recommend against converting from MSpec to NUnit. When introducing people to MSpec I like to start with a "classic" NUnit (PersonRepositoryTester.TestAddNewPerson) fixture, convert it over to a more BDDish fixture like the one below and then show them how MSpec can help to reduce language noise and introduce readability + better reporting.
[TestFixture]
public class When_adding_a_new_person
{
PersonRepository sut;
[TestFixtureSetUp]
public void Establish_and_because()
{
sut = new PersonRepository();
sut.AddPerson("Jim", "Panse");
}
[Test]
public void It_should_have_one_person()
{
Assert.That(sut.Count, Is.EqualTo(1));
}
[Test]
public void It_should_contain_the_new_person()
{
Assert.That(sut.Contains("Jim", "Panse"), Is.True);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
686 次 |
| 最近记录: |