Tuesday, November 24, 2009

The joy of MSpec

I’ve just kicked off a new MVC Framework application today. I’ve been considering moving to more behaviour based testing for a while now and I’ve been intrigued by Aaron Jensen’s descriptions of his MSpec (Machine Specifications) framework. So this afternoon I downloaded the build from here and got coding (just click ‘login as guest’ at the bottom).

It’s very nice. Here’s some tests for my container setup:

using System.Web.Mvc;
using Castle.Windsor;
using Machine.Specifications;
using Spark.Web.Mvc;
using Mike.Portslade.Web.IoC;

namespace Mike.Portslade.Web.Tests.IoC.ContainerManagerSpecs
    public class when_container_is_created
        Because of = () =>
            container = ContainerManager.CreateContainer();

        It should_register_HomeController = () =>

        It should_register_SparkViewFactory = () =>
            container.Kernel.HasComponent(typeof (SparkViewFactory));

        It should_register_an_IControllerFactory = () =>
            container.Kernel.HasComponent(typeof (IControllerFactory));

        private static IWindsorContainer container;

I love the readability of the test code. Sure you have to learn to love the ‘=()=>’, but come on people, lambda syntax is hardly new any more.

When I run this using TestDriven.NET, my test runner of choice for many years now, I get this output:

when container is created
» should register HomeController
» should register SparkViewFactory
» should register an IControllerFactory

The only thing I don’t like is that the ‘It’ has gone missing which is a shame, otherwise this is just what I want from behaviour based testing framework; very low friction and easy to read output.

Well done Aaron, I’m very impressed.


  1. it does look ... clever. I'm not sure that gives me a warm feeling though :)

  2. Thanks! The reason "It" is gone, is because the "It" is actually a placeholder for the Subject, which you have not placed on your tests. If you add a Subject attribute to the class you'll see them in the output and it will make more sense: ContainerManager, when container is created > should register...

  3. I started down the BDD testing path with Cucumber in Rails, so when switching back to .NET I was after something similar. My highest priority was something with the ability to allow step mapping, meaning I can write normal sentences in English which are mapped to the methods being run.

    NBehave does this nicely, as documented here.

  4. Louis de Klerk12:42 pm

    I also recently started with BDD, and reviewed a few frameworks. Aubergine looks promising, but my current choice is SpecFlow. (http://www.specflow.org)

    It is fairly mature, and well integrated with Visual Studio.
    It definitely gave me that "warm feeling".

    Took me less than an hour to learn the ropes - although watching the screencast from the website helped a lot.

    My main use for it is WPF and Silverlight applications written in the MVVM style.

    In short - I'm very impressed, and a definite convert!

  5. Anonymous3:45 pm

    This comment has been removed by a blog administrator.


Note: only a member of this blog may post a comment.