Specification test patterns – reprise

In my previous post, I mentioned I could improve the readability of test classes that require a single context for all assertions. The solution is to use Fixie.

Before Fixie

public class When_I_initialize_People_with_text_that_includes_invalid_data
    : IClassFixture<When_I_initialize_People_with_text_that_includes_invalid_data.Context>
{
    public class Context : Context
    {
        public Context()
        {
            SUT = new People { 
                "{\"firstName\":\"John\", " +
                "\"lastName\":\"Smith\"}",
                "{\"member\":\"John\", " +
                "\"another_member\":\"Smith\"}" 
            };
        }
    }

    private People people;

    public When_I_initialize_People_with_text_that_includes_invalid_data(Context context)
    {
        people = context.SUT;
    }

    [Fact]
    public void It_should_contain_expected_number_of_items()
    {
        people.Count().Should().Be(1);
    }

    [Fact]
    public void It_should_contain_expected_person()
    {
        people.First().ShouldBeEquivalentTo(
            new Person("John", "Smith"));
    }
}

After Fixie

public class When_I_initialize_People_with_text_that_includes_invalid_data
{
    private People people;

    public When_I_initialize_People_with_text_that_includes_invalid_data()
    {
        // Note - I use a custom convention that ensures
        // this arrangement/act is executed once only.
        people = new People {
            "{\"firstName\":\"John\", " +
            "\"lastName\":\"Smith\"}",
            "{\"member\":\"John\", " +
            "\"another_member\":\"Smith\"}"
        };
    }

    [Fact]
    public void It_should_contain_expected_number_of_items()
    {
        people.Count().Should().Be(1);
    }

    [Fact]
    public void It_should_contain_expected_person()
    {
        people.First().ShouldBeEquivalentTo(
            new Person("John", "Smith"));
    }
}

I think its clearer.

There are few problems however:

  • The tests don’t run in parallel which means its slower than xUnit 2.
  • I have to write a custom convention because the one that comes out the box doesn’t support my specification style tests.
  • I decided to use xUnit because I didn’t want to introduce a library that others haven’t heard of or probably even used before – what would they think when I tell them we’re using Fixie now? (I should add that this might just be unique to the place I work. I work with young apprentices that are still finding their feet with testing and their previous experience has been with other xUnit like test frameworks in Java and PHP.)

Fixie or xUnit? What do you think?

The updated code can be found on the Fixie branch here: Specification Test Patterns/Fixie