I am trying to get started with pure TDD and also consider the BDD flavor. I am trying to grasp, how writing unit tests with MSpec feels in contrast pure to NUnit.
Consider a simple MSpec test like the following:
[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;
}
How would you convert this to NUnit in a clean way but without using any BDD extensions or anything. I think that it is a good idea that each should assertion to be one separately runnable test and Establish
and Because
should be executed just once for all should assertions. I could use [Setup]
for Establish
and Because
, but that would be run for each test. I could use Assert.
for It
but that wouldn't make them separately runnable tests.
How would this example contrast to NUnit style?
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);
}
}