FluentTest

Published on Sunday, 21 August 2016

Fluent Unit testing

One thing that has always bothered me about unit testing is there is no nice way to split tests out into their 3 components Arrange, Act and Assert. The idea is you format the test in a way where its obvious to follow but sometimes it gets really messy. One strategy is you can use comments to break it up but I find you will stop putting them in after you start to write a large number of tests.

So I was thinking you could express them as a lambda expression. The other nice thing is in c# 6 there is also the ability to declare a method as a lambda express too.

public void MyMethod() => Console.WriteLine("Hello World");

This opens the door to all interesting types of unit tests. So with this new framework I created you can write tests like this:

[Fact]
public void TestImplimentedWithFluentTest() => new Test().CreateWithSut<TestObject>()
    .Arrange(c => c.Sut = c.Container.Create<TestObject>())
    .Act(c => c.Sut.AProperty = "Example")
    .Assert(c => c.Sut.AProperty == "Example");

Instead of this:

[Fact]
public void TyicalExampleTest()
{
    var container = new Fixture().Customize(new AutoFakeItEasyCustomization());
    var sut = container.Create<TestObject>();

    sut.AProperty = "Example";

    Assert.True(sut.AProperty == "Example");
}

Download/Project Details