I have moved my active blog over to tumblr. I've maintained this blog for reference but will be posting to http://www.robustsoftware.co.uk instead. I've pointed my Feedburner feed to tumblr so if you're subscribed already you should already have switched with me.

BDD from scratch – Build your own framework (Part 2)

In part one I covered how to organise your tests into the Given-When-Then style through the use of an abstract base class. In this post I’m going to show how you can manage your mocks more efficiently. Though not really a part of BDD I’ve found it very useful in clarifying the behaviour described in my tests as it reduces the amount of noise caused by the definition of variables.

I’m going to walk you through an example test, starting off with what we finished off with in part 1. We are going to be testing this standard ASP.NET MVC controller method:

namespace RobustSoftware.BddFromScratch.WebApplication.Controllers

{

    public class BooksController : Controller

    {

        private readonly IBookService bookService;

        private readonly IAuthenticationService authenticationService;

 

        public BooksController(IBookService bookService, IAuthenticationService authenticationService)

        {

            this.bookService = bookService;

            this.authenticationService = authenticationService;

        }

 

        public ViewResult YourBooks()

        {

            var currentUser = authenticationService.CurrentUser();

            ViewData.Model = bookService.OwnedBy(currentUser);

            return View("YourBooks");

        }

    }

}

I’m going to be using Moq as my mocking framework for this test; again I’ve applied the same concept to other frameworks such as Rhino Mocks. Here is how our test starts out:

namespace RobustSoftware.BddFromScratch.WebApplication.Test.Controllers.Books

{

    public class DisplayingYourBooks : BehaviourTest

    {

        private MockFactory factory;

        private BooksController controller;

        private Mock<IBookService> bookService;

        private Mock<IAuthenticationService> authenticationService;

        private ViewResult result;

        private List<Book> yourBooks;

        private User currentUser;

 

        protected override void Given()

        {

            factory = new MockFactory(MockBehavior.Loose);

 

            bookService = factory.Create<IBookService>();

            authenticationService = factory.Create<IAuthenticationService>();

 

            yourBooks = new List<Book>();

            currentUser = new User();

 

            authenticationService.Setup(x => x.CurrentUser()).Returns(currentUser);

            bookService.Setup(x => x.OwnedBy(currentUser)).Returns(yourBooks);

 

            controller = new BooksController(bookService.Object, authenticationService.Object);

        }

 

        protected override void When()

        {

            result = controller.YourBooks();

        }

 

        [Then]

        public void ShownYourBooksView()

        {

            Assert.AreEqual("YourBooks", result.ViewName);

        }

 

        [Then]

        public void PassedListOfYourBooks()

        {

            Assert.AreSame(yourBooks, result.ViewData.Model);

        }

    }

}

The obvious step to reduce the amount of test code on display would be to move the setup of the MockFactory up into the base class. We’re going to take it a step further than that though and introduce a mock management class. Here is what it looks like:

namespace RobustSoftware.BddFromScratch.Framework

{

    public class MockManager

    {

        private readonly MockFactory factory;

        private readonly IDictionary<Type, object> mockDictionary;

 

        public MockManager()

        {

            factory = new MockFactory(MockBehavior.Loose);

            mockDictionary = new Dictionary<Type, object>();

        }

 

        public Mock<T> Mock<T>() where T : class

        {

            var type = typeof(T);

 

            if (!mockDictionary.ContainsKey(type))

            {

                mockDictionary.Add(type, factory.Create<T>());

            }

 

            return mockDictionary[type] as Mock<T>;

        }

    }

}

This is a wrapper around a dictionary of mock objects. When we ask for a mock that has not been requested before, one is created. Otherwise, the previously created mock is retrieved from the dictionary and returned. We’ll add the creation of the mock manager to our abstract base class and expose the Mock<T> method for use within our tests:

namespace RobustSoftware.BddFromScratch.Framework

{

    [TestFixture]

    public abstract class BehaviourTest

    {

        private MockManager mockManager;

 

        protected Mock<T> Mock<T>() where T : class

        {

            return mockManager.Mock<T>();

        }

 

        [TestFixtureSetUp]

        public void Setup()

        {

            mockManager = new MockManager();

 

            Given();

            When();

        }

 

        protected abstract void Given();

        protected abstract void When();

    }

}

Now we can utilise the mock manager within our original test, reducing the lines of code considerably:

namespace RobustSoftware.BddFromScratch.WebApplication.Test.Controllers.Books

{

    public class DisplayingYourBooks : BehaviourTest

    {

        private BooksController controller;

        private ViewResult result;

        private List<Book> yourBooks;

        private User currentUser;

 

        protected override void Given()

        {

            yourBooks = new List<Book>();

            currentUser = new User();

 

            Mock<IAuthenticationService>().Setup(x => x.CurrentUser()).Returns(currentUser);

            Mock<IBookService>().Setup(x => x.OwnedBy(currentUser)).Returns(yourBooks);

 

            controller = new BooksController(Mock<IBookService>().Object, Mock<IAuthenticationService>().Object);

        }

 

        protected override void When()

        {

            result = controller.YourBooks();

        }

 

        [Then]

        public void ShownYourBooksView()

        {

            Assert.AreEqual("YourBooks", result.ViewName);

        }

 

        [Then]

        public void PassedListOfYourBooks()

        {

            Assert.AreSame(yourBooks, result.ViewData.Model);

        }

    }

}

As we no longer have to worry about variable declaration and assignment for our mocks, we can leave the test code to signal the intent of our test. As there is no real setup for the fields currentUser and yourBooks, I’d be tempted to in-line those variables but this is a matter of personal taste:

namespace RobustSoftware.BddFromScratch.WebApplication.Test.Controllers.Books

{

    public class DisplayingYourBooks : BehaviourTest

    {

        private BooksController controller;

        private ViewResult result;

        private List<Book> yourBooks = new List<Book>();

        private User currentUser = new User();

 

        protected override void Given()

        {

            Mock<IAuthenticationService>().Setup(x => x.CurrentUser()).Returns(currentUser);

            Mock<IBookService>().Setup(x => x.OwnedBy(currentUser)).Returns(yourBooks);

 

            controller = new BooksController(Mock<IBookService>().Object, Mock<IAuthenticationService>().Object);

        }

 

        protected override void When()

        {

            result = controller.YourBooks();

        }

 

        [Then]

        public void ShownYourBooksView()

        {

            Assert.AreEqual("YourBooks", result.ViewName);

        }

 

        [Then]

        public void PassedListOfYourBooks()

        {

            Assert.AreSame(yourBooks, result.ViewData.Model);

        }

    }

}

As you can see, this reduces the amount of code required to establish the same context to test a given behaviour. The less lines of code you need to set up a test, the easier it is going to be to understand that test in the future. This, along with the more explanatory naming of your test, makes it much easier to maintain that test in the future.

Next up, I’ll show how we can utilise our MockManager to implement auto mocking. This will reduce the amount of code in our test slightly, but more importantly it makes our test suite less brittle.

Related posts

BDD from scratch – Build your own framework (Part 1)

BDD is a higher level of unit testing, it creates better documentation of your system by recording its intent which makes your system easier to learn for new developers and relearn for when you revisit your code further down the line.

I’m going to show you how to build your own BDD testing framework on top of a vanilla unit testing framework. For my example I’m going to use NUnit but I’ve applied the same principles with MbUnit, xUnit and it should work with any other unit testing framework too.

What’s good about doing things this way, as opposed to using a purpose built BDD framework like MSpec, is that it allows your tests to exist and be run side-by-side with traditional unit tests. This can ease the migration to the new style if you’re all moving over to it and it lets you write tests in the BDD style for some parts of the system whilst sticking with the traditional unit style for other parts. Though I’ll be amazed if you don’t end up writing all your tests in the BDD style as everyone I’ve shown it too loves it.

I’ll show you how to create the basis for your own BDD framework and I’ll give an example of how a BDD style test looks in comparison to a traditional unit test.

What is BDD all about?

The core concept of BDD is Given-When-Then (often referred to as Arrange-Act-Assert):

  1. Given the system is in a certain state (the context of the test)
  2. When an action is performed on the system (normally calling a single method)
  3. Then a list of assertions should be satisfied

Herein lies another fundamental difference with BDD, you will create a test fixture per scenario rather than a fixture per class as it common with traditional unit testing.

The basis of our framework

The way to transform your standard unit testing framework to a BDD one is to use an abstract base class. This will modify how your tests are run, giving you the ability to specify your Given, When and corresponding Thens separately from one another:

using NUnit.Framework;

 

namespace RobustSoftware.BddFromScratch.Framework

{

    [TestFixture]

    public abstract class BehaviourTest

    {

        [TestFixtureSetUp]

        public void Setup()

        {

            Given();

            When();

        }

 

        protected abstract void Given();

        protected abstract void When();

    }

}

This means that the contents of your Given and When methods will be run once before each of your methods decorated with the Test attribute are run. This is important as it means that all your assertions should not have side effects (this should be the case already). You will use it in a test class by doing something like this:

using System;

using NUnit.Framework;

 

namespace RobustSoftware.BddFromScratch.Framework

{

    public class ExampleBehaviour : BehaviourTest

    {

        protected override void Given()

        {

            // the system is setup in a certain state

        }

 

        protected override void When()

        {

            // a defined action is performed on the system

        }

 

        [Test]

        public void ThisAssertionShouldBeSatisfied()

        {

            Assert.IsTrue(true);

        }

 

        [Test]

        public void AnotherAssertionShouldBeSatisfied()

        {

            Assert.IsTrue(true);

        }

    }

}

Notice that the intent verified by each assertion is documented in the name of the test method. This means that when you run your tests, the name of the tests themselves show you when was meant to happen rather than the error messages that most people forget to add to their assertions.

Another thing that I like to do but is entirely optional is alias the Test attribute to be able to use a Then attribute with the same behaviour:

using NUnit.Framework;

 

namespace RobustSoftware.BddFromScratch.Framework

{

    public class ThenAttribute : TestAttribute

    {

    }

}

This just changes the original example test slightly so it’s more obvious how the Given-When-Then style is being applied:

using System;

using NUnit.Framework;

 

namespace RobustSoftware.BddFromScratch.Framework

{

    public class ExampleBehaviour : BehaviourTest

    {

        protected override void Given()

        {

            // the system is setup in a certain state

        }

 

        protected override void When()

        {

            // a defined action is performed on the system

        }

 

        [Then]

        public void ThisAssertionShouldBeSatisfied()

        {

            Assert.IsTrue(true);

        }

 

        [Then]

        public void AnotherAssertionShouldBeSatisfied()

        {

            Assert.IsTrue(true);

        }

    }

}

Converting a traditional test

I’ll start of with what I think to be a pretty standard unit test that I found in the source of OpenRasta:

[Test]

public void a_change_after_a_creation_results_in_a_new_oject_with_the_same_properties()

{

    var binder = new KeyedValuesBinder("customer", typeof(Customer));

    binder.SetProperty("username", new[] { "johndoe" }, (str, type) => BindingResult.Success(str));

    binder.BuildObject();

 

    binder.SetProperty("firstname", new[] {"john"}, (str, type) => BindingResult.Success(str));

    var customer = (Customer)binder.BuildObject().Instance;

 

    customer.Username.ShouldBe("johndoe");

    customer.FirstName.ShouldBe("john");

}

As I see it this, like many other traditional unit tests, is already split into the Given-When-Then style logically:

[Test]

public void a_change_after_a_creation_results_in_a_new_oject_with_the_same_properties()

{

    // Given - the context we are establishing in the system

    var binder = new KeyedValuesBinder("customer", typeof(Customer));

    binder.SetProperty("username", new[] { "johndoe" }, (str, type) => BindingResult.Success(str));

    binder.BuildObject();

 

    binder.SetProperty("firstname", new[] {"john"}, (str, type) => BindingResult.Success(str));

 

    // When - the action we are performing on the system

    var customer = (Customer)binder.BuildObject().Instance;

 

    // Then - checking the system ends up in the desired state

    customer.Username.ShouldBe("johndoe");

    customer.FirstName.ShouldBe("john");

}

So splitting it physically into the separate sections is not much of a leap:

public class ChangingAnObjectAfterACreation : BehaviourTest

{

    private KeyedValuesBinder binder;

    private Customer customer;

 

    protected override void Given()

    {

        binder = new KeyedValuesBinder("customer", typeof(Customer));

        binder.SetProperty("username", new[] { "johndoe" }, (str, type) => BindingResult.Success(str));

        binder.BuildObject();

 

        binder.SetProperty("firstname", new[] { "john" }, (str, type) => BindingResult.Success(str));

    }

 

    protected override void When()

    {

        customer = (Customer) binder.BuildObject().Instance;

    }

 

    [Then]

    public void UsernameShouldBeJohnDoe()

    {

        Assert.AreEqual("johndoe", customer.Username);

    }

 

    [Then]

    public void FirstNameShouldBeJohn()

    {

        Assert.AreEqual("john", customer.FirstName);

    }

}

I've dropped the use of the assertion extension methods that Sebastien Lambla was using (I’ll cover how to write those later in the series), but otherwise the actual test code has remained the same.

The physical separation of the logical parts of the test makes the test easier to follow and the assertions at least as explicit even without the extension methods being used.

The fact that BDD makes the separation many people already make when writing traditional unit tests more explicit is why I and other people like it. It makes your tests more readable as you can often skim the Given section as that should be conveyed by the name of the entire fixture. That leaves you to see what method is being called and the assertions being made. These are often now more descriptive as they are given a method name.

If you’ve got any questions, leave a comment and I’ll reply as soon as I can. Do the same if you’ve any related topics you’d like to see covered. I’m planning on covering, mock management, auto-mocking, fluent test extensions and shared contexts.

Related posts