Category Archives: nunit

Contract testing with NUnit

When you’re testing your code, you’re most likely doing different types of tests – e.g. you might be doing real unit testing, integration testing, etc. In Rebus, we had the need to do contract tests1, which is a test that ideally runs on multiple implementations of some interface, verifying that each implementation – given various preconditions in the form of differing setups etc – is cabable of satisfying a contract in the form of an interface.

In other words, for each implementation of a given interface, answer this question: Can the implementation behave in the same way in regards to the behavior we care about.

I’m aware of Grensesnitt, which Greg Young authored some time ago – and that is exactly what I’m after, only without the depending on an additional library, without the automatic stuff, etc. I just wanted to do it manually.

So, at first I wrote a lot of tedious code that would execute multiple tests for each test case, which was really clunky and tedious and would yield bad error messages when tests would fail, etc. Luckily, Asger Hallas brought my attention to the fact that a much more appropriate mechanism was already present in NUnit that would enable the thing that I wanted.

It turned out that NUnit’s [TestFixture] attribute accepts a Type as an argument, i.e. you can do this: [TestFixture(typeof(SomeType))], which will cause the test fixture type, which should be generic, to be closed with the given type. E.g. like so:

[TestFixture(typeof(SomeClassSomewhere))]
public class MyTestFixture<T>
{
    [Test]
    public void CheckType()
    {
        // so, what to do with T in here?
        Assert.That(typeof(T), Is.EqualTo(typeof(SomeClassSomewhere)));
    }
}

Now, if we add a generic constraint to the fixture’s type parameter, we can require that the type is a factory and can be newed up:

[TestFixture(typeof(SomeClassSomewhere))]
public class MyTestFixture<T> where T : ISomeParticularInterface, new()
{
    [Test]
    public void CheckType()
    {
        // oh wow, now we can do stuff with T
        ISomeParticularInterface t = new T();
 
        t.CallSomethingFromTheInterface();
    }
}

See where this is going? Now one single test fixture can be closed with multiple factory types, where each factory type is capable of creating an instance of one particular implementation of something, that should be tested. E.g. in Rebus, we can test that multiple implementations of the ISendMessages and IReceiveMessages work as expected by creating a factory interface like this:

public interface ITransportFactory
{
    Tuple<ISendMessages, IReceiveMessages> Create();
    void CleanUp();
}

where an implementation might look like this:

public class RabbitMqTransportFactory : ITransportFactory
{
    RabbitMqMessageQueue sender;
    RabbitMqMessageQueue receiver;
 
    public Tuple<ISendMessages, IReceiveMessages> Create()
    {
        sender = new RabbitMqMessageQueue(RabbitMqFixtureBase.ConnectionString, "tests.contracts.sender", "tests.contracts.sender.error");
        receiver = new RabbitMqMessageQueue(RabbitMqFixtureBase.ConnectionString, "tests.contracts.receiver", "tests.contracts.receiver.error");
 
        return new Tuple<ISendMessages, IReceiveMessages>(sender, receiver);
    }
 
    public void CleanUp()
    {
        sender.Dispose();
        receiver.Dispose();
    }
}

and one particular test fixture might look like this:

[TestFixture(typeof(MsmqTransportFactory))]
[TestFixture(typeof(RabbitMqTransportFactory)), Category(TestCategories.Rabbit)]
public class TestSendAndReceive<TFactory> : FixtureBase where TFactory : ITransportFactory, new()
{
    static readonly TimeSpan MaximumExpectedQueueLatency = TimeSpan.FromMilliseconds(300);
 
    TFactory factory;
    ISendMessages sender;
    IReceiveMessages receiver;
 
    protected override void DoSetUp()
    {
        factory = new TFactory();
 
        var transports = factory.Create();
        sender = transports.Item1;
        receiver = transports.Item2;
    }
 
    protected override void DoTearDown()
    {
        factory.CleanUp();
    }
 
    /// ....and then there's test cases down here working on sender and receiver

The advantage is that it’s much easier to see if new implementations of Rebus’ abstractions can satisfy the required contracts – and with the way it’s implemented now, various implementations can have different setup and teardown code, and their fixtures can belong to a suitable NUnit category, allowing Rebus’ build servers to run only tests where the required infrastructure is in place.

  1. I’m not aware if there’s a more established word for this kind of black box test.

Using Windsor as an auto-mocking container

One of my former colleagues blogged about using AutoFixture as an auto-mocking container the other day, which got me thinking about auto-mocking with Windsor.

Although I’ve never used auto-mocking myself, a few months ago, I answered a question on StackOverflow, hinting at how auto-mocking could be accomplished with Castle Windsor. I didn’t provide any example code though, so this post will show a more complete solution on how auto-mocking can be implemented with Windsor and Rhino Mocks1.

A lazy component loader to generate the mocks

First, I create a simple ILazyComponentLoader that lazily registers a Rhino Mocks instance when a particular service is requested – like so:

public class AutoMockingLazyComponentLoader : ILazyComponentLoader
{
    public IRegistration Load(string key, Type service, IDictionary arguments)
    {
        return Component.For(service).Instance(MockRepository.GenerateMock(service, new Type[0]));
    }
}

- real simple. Windsor’s default lifestyle is singleton, which ensures that subsequent calls for the same service will give me the same mock instance.

Test fixture base class

Then I create a test fixture base class, which is supposed to act as exactly that: a fixture for the SUT:

public abstract class AutoMockingFixtureFor<TAppService>
{
    IWindsorContainer container;
 
    protected TAppService service;
 
    [SetUp]
    public void SetUp()
    {
        container = new WindsorContainer()
            .Register(Component.For<ILazyComponentLoader>().ImplementedBy<AutoMockingLazyComponentLoader>(),
                      Component.For<TAppService>());
 
        // build sut and inject mocks for all dependencies
        service = container.Resolve<TAppService>();
 
        DoSetUp();
    }
 
    protected virtual void DoSetUp() { }
 
    [TearDown]
    public void TearDown()
    {
        DoTearDown();
 
        container.Dispose()
    }
 
    protected virtual void DoTearDown() { }
 
    protected TDependency Dep<TDependency>()
    {
        return container.Resolve<TDependency>();
    }
 
    protected TMock Mock<TMock>() where TMock : class
    {
        return MockRepository.GenerateMock<TMock>();
    }
}

As you can see, my SetUp method creates a new WindsorContainer, registering nothing but my AutoMockingLazyComponentLoader and TAppService – the SUT type. It then uses the container to instantiate the SUT, storing it away in a protected instance variable for test cases to work on.

I included DoSetUp and DoTearDown methods for my test fixtures to override in case they need to – but in most cases, they should not be used because of the coupling they introduce between test cases.

Lastly, I have two methods: Dep, which allows me to access an injected service type (short for “dependency”), and Mock which allows me to generate a new mock object in case I need to mock something other than my SUT’s dependencies.

How to write a new test fixture

As a result of this, a test fixture for something called HandleUpdateDisturbanceForecast is reduced to something like this:

[TestFixture]
public class TestOfHandleUpdateDisturbanceForecast : AutoMockingFixtureFor<HandleUpdateDisturbanceForecast>
{
    [Test]
    public void UpdatesDriverWithReceivedObservableForecast()
    {
        // Arrange
        var now = 19.March(1979).At(12.Hours());
        var localUnit = new GenericDriver();
        Dep<IProvidesSiteToStartUp>().Stub(p => p.GetLocalUnitByAlias("ali")).Return(localUnit);
 
        // Act
        service.Handle(new UpdateDisturbanceForecast
                            {
                                LocalUnitAlias = "ali",
                                DisturbanceForecast = new List<DisturbancePlanEntry>
                                                            {
                                                                new DisturbancePlanEntry {Time = now, Disturbance = 2},
                                                            }
                            });
 
        // Assert
        var forecast = localUnit.DisturbanceForecast;
        Assert.AreEqual(1, forecast.Count);
        Assert.AreEqual(now, forecast[0].Time);
        Assert.AreEqual(2, forecast[0].Value);
    }
}

- which I think looks pretty slick. And yes, this is an actual test case from something we’re building – doesn’t matter what it’s doing, just wanted to show an actual example.

It’s not like I’m saving a huge amount of coding here – I usually only instantiate my SUT in the SetUp method of my test fixtures, but I like how the “fixed style” of AutoMockingFixtureFor encourages application services to follow a pattern that makes for easy IoC and testing. And the fixture is relieved of almost all clutter that does not directly relate to the thing being tested.

  1. I realize that all the cool kids are using other mocking libraries these days. I’m still using Rhino though, but it should be a fairly trivial task to “port” this solution to another mocking library.

Shouldly – better assertions

Today I came across Shouldly, as I followed a link in a tweet by Rob Conery. I have a thing for nifty mini-projects, so I immediately git clone http://github.com/shouldly/shouldly.git‘d it, and pulled it into a small thing I am building.

What is Shouldly? Basically, it’s just a niftier way of writing assert statements in tests. It fits right in between NUnit and Rhino Mocks, so you will get the most out of it if you are using those two for your unit tests. Check out this repository test – first, arrange and act:

var notes = new Notes{Artist="Josh Rouse", Title="Winter In The Hamptons"};
repo.Save(notes);
var id = notes.Id;
 
var loadedNotes = repo.FindOne(id);

and then, usually the assert would look something like this:

Assert.AreEqual("Joe Rouse", loadedNotes.Artist);
Assert.AreEqual("Winter In The Hamptons", loadedNotes.Title);

- yielding error messages like this:

NUnit.Framework.AssertionException:   Expected string length 9 but was 10. Strings differ at index 2.
  Expected: "Joe Rouse"
  But was:  "Josh Rouse"
  -------------^

which is probably OK and acceptable, because how would NUnit know any better than that?

Check out what Shouldly can do:

loadedNotes.Artist.ShouldBe("Joe Rouse");
loadedNotes.Title.ShouldBe("Winter In The Hamptons");

- yielding error messages like this:

NUnit.Framework.AssertionException: loadedNotes.Artist
        should be
    "Joe Rouse"
        but was
    "Josh Rouse"

which IMO is just too nifty to ignore!

Shouldly takes advantage of the fact the the current StackTrace has all the information we’re after when the assert is an extension method, which is extremely cool and well thought out.

Moreover, it integrates with Rhino Mocks, yielding better messages when doing AssertWasCalled stuff: Instead of just telling that the test did not pass, it tells you exactly which calls were recorded and which one was expected – a thing that Rhino Mocks has always been missing.

Conclusion: YourNextProject.ShouldContain("Shouldly")

Working with Windsor and ASP.NET MVC

In the previous post I showed how easy it is to install an IoC container at the system boundary of your ASP.NET MVC application and have it resolve everything from there.

But what I did not show, was where the container came from – in the example, the container just pops out of nowhere on this line:

static IContainer myContainer = GetContainerInstanceFromSomewhere();

So how can we implement GetContainerInstanceFromSomewhere()?

Well, I like to do it like this:

IWindsorContainer GetContainerInstanceFromSomewhere()
{
    return new WindsorContainer("windsor.config");
}

Isn’t that easy? And then I have a folder structure in my ASP.NET MVC project that looks like this:

config-folder

It can be seen that I have a folder for each configuration of the system (development, test, prod) and one containing configuration files that are common for all configurations. For each configuration I have a hibernate.cfg.xml to configure NHibernate and a windsor.config which is loaded by the Windsor container in each particular configuration.

My development/windsor.config looks like this:

<castle>
  <include uri="file://controllers.config"/>
 
  <include uri="file://facilities.config"/>
 
  <include uri="file://factories.config"/>
 
  <include uri="file://repositories.config"/>
 
  <include uri="file://services.config"/>
 
  <!-- For this configuration only -->
  <components>
    <component id="services.ApplicationSettings"
               service="Model.Services.IApplicationSettings, Model.Services"
               type="WebSite.Stuff.DevelopmentMachineApplicationSettings, WebSite">
      <parameters>
        <DefaultEmailSender>noreply@website.com</DefaultEmailSender>
        <UrlBase>http://localhost:1766</UrlBase>
      </parameters>
    </component>
 
    <component id="services.EmailSender"
               service="Model.Services.IEmailSender, Model.Services"
               type="WebSite.Stuff.FakeLoggingEmailSender, WebSite" />
 
  </components>
</castle>

It can be seen that my development configuration is used to Cassini running on port 1766 :) moreover, it can be seen that my development configuration is using a fake, logging email sender, which – much as you would expect – only logs the content from emails that would otherwise have been sent.

Of course, the configuration files will not automatically be available to the web application the way they are organized now – therefore, my web project has a post-build task with the following two lines:

xcopy "$(ProjectDir)Config\common\*.*" "$(TargetDir).." /y
xcopy "$(ProjectDir)Config\development\*.*" "$(TargetDir).." /y

- thus copying the common configuration files along with the development configuration files to the base directory of the web application.

This also means that my build script must overwrite the development configuration files when building the system in a deployment configuration. It can be achieved as simple as this (using MSBuild):

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <ApplicationFiles Include="ProjectName\WebSite\Default.aspx" />
    <ApplicationFiles Include="ProjectName\WebSite\Global.asax" />
    <ApplicationFiles Include="ProjectName\WebSite\Global.asax.cs" />
    <ApplicationFiles Include="ProjectName\WebSite\web.config" />
 
    <ApplicationFiles Include="ProjectName\WebSite\bin\*.*" />
  </ItemGroup>
 
  <PropertyGroup>
    <DestinationFolder>C:\Release\ProjectName</DestinationFolder>
    <ConfigurationFolder>ProjectName\WebSite\Config</ConfigurationFolder>
  </PropertyGroup>
 
  <Target Name="build">
    <MSBuild Projects="ProjectName\WebSite.sln" Targets="build" StopOnFirstFailure="true" Properties="Configuration=Release;">
      <Output TaskParameter="TargetOutputs" ItemName="AssembliesBuiltByChildProjects" />
    </MSBuild>
  </Target>
 
  <Target Name="deploy">
    <RemoveDir Directories="$(DestinationFolder)\Views;$(DestinationFolder)\Content;$(DestinationFolder)\bin;$(DestinationFolder)" />
    <MakeDir Directories="$(DestinationFolder);$(DestinationFolder)\bin;$(DestinationFolder)\Views;$(DestinationFolder)\Content" />
 
    <Copy SourceFiles="@(ApplicationFiles)" DestinationFolder="$(DestinationFolder)"/>
    <Exec Command="xcopy /d/e/f/y ProjectName\WebSite\Views\*.* $(DestinationFolder)\Views" />
    <Exec Command="xcopy /d/e/f/y ProjectName\WebSite\Content\*.* $(DestinationFolder)\Content" />
  </Target>
 
  <Target Name="deploy_common_configuration_files">
    <Exec Command="xcopy /d/e/f/y $(ConfigurationFolder)\common\*.* $(DestinationFolder)" />
  </Target>
 
  <!-- Deployment configurations -->
  <Target Name="deploy_test" DependsOnTargets="build;deploy;deploy_common_configuration_files">
    <Exec Command="xcopy /d/e/f/y $(ConfigurationFolder)\test\*.* $(DestinationFolder)" />
  </Target>
 
  <Target Name="deploy_prod" DependsOnTargets="build;deploy;deploy_common_configuration_files">
    <Exec Command="xcopy /d/e/f/y $(ConfigurationFolder)\prod\*.* $(DestinationFolder)" />
  </Target>
</Project>

Here I have define tasks for compiling the web site (“build”), deploying the binaries + views + content files (“deploy”), deploying common configuration files (“deploy_common_configuration_files”), and then one task for each deployable configuration: “deploy_test” and “deploy_prod”. This makes deploying the web site on my test web server as easy as running the following command:

msbuild /t:deploy_test

What is left now, is to make sure that the different sets of configuration files are valid in the sense that Windsor can resolve everything right. That is easily accomplished in the following NUnit test:

[TestFixture]
public class TestWindsorCanResolve
{
	const string ConfigFileRoot = @"..\..\..\WebSite\Config";
 
	[Test]
	public void CanMakeControllerInstances_test()
	{
		CheckConfiguration("test");
	}
 
	[Test]
	public void CanMakeControllerInstances_prod()
	{
		CheckConfiguration("prod");
	}
 
	static void CheckConfiguration(string configuration)
	{
		var controllerTypes = GetControllerTypes();
		var container = GetContainer(configuration);
 
		// using the given controllers and the given Windsor container - make sure
		// every controller can be instantiated
		controllerTypes.ForEach(t => container.Resolve(t.Name.Substring(0, t.Name.LastIndexOf("Controller")).ToLower()));
	}
 
	static IWindsorContainer GetContainer(string configuration)
	{
		// get path to configuration to test (inside the web site project)
		var path = ConfigFileRoot + Path.DirectorySeparatorChar 
				+ configuration + Path.DirectorySeparatorChar 
				+ "windsor.config";
 
		// configure Windsor
		var windsorContainer = new WindsorContainer(new XmlInterpreter(path));
 
		return windsorContainer;
	}
 
	static List<Type> GetControllerTypes()
	{
		// get assembly with controllers
		var assembly = Assembly.GetAssembly(typeof (HomeController));
 
		var types = assembly.GetTypes();
 
		// get all controller types
		return new List<Type>(types)
			.FindAll(t => typeof (IController).IsAssignableFrom(t)
			              && !t.IsAbstract);
	}
}

This way, if I introduce a service in my development configuration that should have been implemented by a production version of the service in my production configuration, I will immediately know about it.

I think this post covers an easy and pragmatic way to control multiple configurations with Windsor and ASP.NET MVC. Of course you might want to split out the configuration-specific parts into multiple files instead of having only a windsor.config for each configuration. What I mean is something like this:

<castle>
  <include uri="file://controllers-common.config"/>
  <include uri="file://controllers.config"/>
 
  <include uri="file://facilities-common.config"/>
  <include uri="file://facilities.config"/>
 
  <include uri="file://factories-common.config"/>
  <include uri="file://factories.config"/>
 
  <include uri="file://repositories-common.config"/>
  <include uri="file://repositories.config"/>
 
  <include uri="file://services-common.config"/>
  <include uri="file://services.config"/>
</castle>

The great thing is that we can refactor our configuration with confidence because of our test. And if we want to be extra-certain that everything works as expect, we might begin to add assertions like this:

public class TestWindsorCanResolve
{
	// (...)
 
	[Test]
	public void OnlyProductionEnvironmentCanSpamOurSensitiveClients()
	{
		Assert.IsTrue(GetContainer("development").Resolve<IEmailSender>() is FakeLoggingEmailSender);
		Assert.IsTrue(GetContainer("test").Resolve<IEmailSender>() is FakeLoggingEmailSender);
	}

Helping future me

Usually, when writing code, you adhere to some conventions on how your stuff should work. At least I hope you do – otherwise your code is probably a mess!

Sometimes, these conventions can be enforced by using some patterns to allow for compile-time checking – one example, that I can think of right now, is using the visitor pattern to implement multiple dispatch, which is explored a little bit in another post.

But what about conventions, that can only be checked at runtime? Well, how do we usually check stuff that can only be checked at runtime? – by writing tests, of course!

One project I am currently involved in, is written in ASP.NET MVC. All form posts are done using the automatic binding features of the framework, and I am following the convention that the names of my view models should end in “Form” – so as to enabling me to easily distinguish my form posting DTOs from my other view models. What is more natural, then, than performing the following test:

[Test]
public void ActionParametersAreEitherPrimitiveTypesOrTruePocos()
{
	var assembly = Assembly.GetAssembly(typeof (HomeController));
	var types = assembly.GetTypes().ToList();
 
	types
		.FindAll(ThatIsConcreteController)
		.SelectMany(t => t.GetMethods().ToList().FindAll(ControllerActions))
		.ToList()
		.ForEach(CheckMethodInfo);
}
 
bool ControllerActions(MethodInfo info)
{
	return info.IsPublic && typeof(ActionResult).IsAssignableFrom(info.ReturnType);
}
 
void CheckMethodInfo(MethodInfo info)
{
	var parameters = info.GetParameters().ToList();
 
	parameters.ForEach(p => CheckParameterType(info, p));
}
 
void CheckParameterType(MethodInfo methodInfo, ParameterInfo parameterInfo)
{
	Assert.IsTrue(IsPrimitiveType(parameterInfo) || IsFormType(parameterInfo),
	              string.Format(
	              	"Action {0} of {1} has parameter of type {2} which is invalid. Use only true pocos from the view models assembly.",
	              	methodInfo.Name,
	              	methodInfo.DeclaringType.Name,
	              	parameterInfo.ParameterType.Name));
}
 
bool IsFormType(ParameterInfo info)
{
	var type = info.ParameterType;
 
	return
		type.Assembly == Assembly.GetAssembly(typeof (LogInForm))
		&& type.Name.EndsWith("Form");
}
 
bool IsPrimitiveType(ParameterInfo info)
{
	// add mores types here if they should be allowed as well
	return
		new List<Type>
			{
				typeof (int),
				typeof (string),
				typeof(int?),
				typeof(Guid)
			}.Contains(info.ParameterType);
}
 
bool ThatIsConcreteController(Type type)
{
	return typeof(Controller).IsAssignableFrom(type) && !type.IsAbstract;
}

That is, I am running through all controller types, getting all actions, and checking the the parameter types are either in the array of accepted types (IsPrimitiveType) or a “true poco” (which in this application is a simple view model whose name ends with “Form” and comes from the right assembly).

This way, I will always know which types are used to deserialize forms. Great! But what about that pesky MissingMethodException whenever I forget to provide a public default contructor in my form models? Easy as cake! That part is checked by the following test:

[Test]
public void AllFormModelsHavePublicDefaultConstructor()
{
	var assembly = Assembly.GetAssembly(typeof (LogInForm));
	var types = assembly.GetTypes().ToList();
 
	types
		.FindAll(ThatCouldBePoco)
		.FindAll(ThatSatisfiesPocoNamingConvention)
		.ForEach(AssertHasPublicDefaultConstructor);
}
 
bool ThatCouldBePoco(Type type)
{
	return type.IsClass
	       && !type.IsAbstract;
}
 
bool ThatSatisfiesPocoNamingConvention(Type type)
{
	var name = type.Name;
 
	return name.EndsWith("Form");
}
 
void AssertHasPublicDefaultConstructor(Type type)
{
	var constructors = type.GetConstructors().ToList();
 
	Assert.IsTrue(constructors.Exists(IsPublicDefaultConstructor),
	              string.Format("Poco type {0} does not provide a public default constructor.",
	                            type.Name));
}
 
bool IsPublicDefaultConstructor(ConstructorInfo info)
{
	var parameters = info.GetParameters();
 
	return parameters.Length == 0;
}

These two tests combined, will assert that nothing will go wrong when submitting forms in my ASP.NET MVC project. That’s just nifty! And I really like the notion that I am helping future me.

Nifty web site with ASP.NET MVC Part 1

[this post is outdated - too much has happened since the first CTP]

This is the first post in a series of at least four about ASP.NET MVC, which I am planning. The series will show a way to build a nifty web site with a tidy, sound, and scalable architecture. ASP.NET MVC will be used to structure the web site, Castle ActiveRecord will be used for persistence, Castle Windsor for dependency injection, NUnit and NMock for testing, NHaml for the views, and principles from agile, domain-driven design, and test-driven development will be used. The series assumes that you want to use the model-view-controller pattern, so I will not try to convince you that it is a great way to structure a web site :-) – even though it is, and currently in my opinion the only sane way to make websites when they contain more than one page…

ASP.NET MVC is a part of the ASP.NET 3.5 Extensions, which is currently only available as a preview. Thus, the details might turn out to be a little off, but still most of the stuff we go through here will apply.

As this is the first post in the series, we will start out by creating a the solution and the project structure – just to get going.
Continue reading