This post will be the first in a series that will cover how to unit test your Umbraco website. The first step to unit testing any project is implementing an Inversion Of Control container within your project. There are a number of IOC frameworks available; from my experience StructureMap is pretty easy to use and implement so it's as good as any. In today's post, I will be covering how to set-up structure map.
Installing The StructureMap Package
The first thing we need to do is install structure map into your web project. Luckily, there's an MVC package that removes all the hassle of setting structure map up for us, in Visual Studio go to the Nugent Package Manager and find the 'StructureMap.MVC5' (assuming you're running MVC5). Change the version of MVC to the one you want to run.
Creating Some Dependencies
The next step is to create some very basic dependencies so we can test we have everything set-up correctly. To do that let's create two classes, an interface and a concrete implementation of that interface.
Configuring Structure Map
The next step is to configure structure map to use our new interfaces. When you installed the Nuget package, a number of classes will have been installed in your project. The main one you care about, as a developer, is the file called 'IOC.cs' found in a folder called 'Dependencies'. When you first look in Ioc.cs you should see something like this:
What we need to do is register our interface with structure map and configure it to pass in our concrete class 'UmbracoDependencies'. To do this we do:
The line that does all the magic is:
This is the bit that configures structure map. When you want to add in your own dependencies, just rinse and repeat. The part above is also quite important:
What this is telling structure map, that on application start, scan all the classes in the same assembly and for any interface that is called 'ISomething', try to find a default concrete implementation called 'Something'. Lastly, the last time I tried implementing this code I got an error about 'TheCallingAssembly'. If you get the same issue you simply need to add this using statement into the IOC.cs
Testing It Works
I'm assuming you have a homepage controller configured and running:
When you run your web project the constructor will be called and the concrete class called '' will be passed in. If you haven't set structure map up correctly you will see a 'No parameterless constructor defined for this object' error message. This means that structure map can't inject the dependency for whatever reason. Most of the time it's either bad configuration in the Ioc.cs
In today's guide, we've gone through all the steps in order to enable dependency injection within our Umbraco website. This tutorial on its own isn't very useful, but the steps in here are the building blocks to allow us to do more useful things like unit test our code later on. To get started with structure map in Umbraco, install the StructureMap.MVC5 package. This does all the hard work for you so all you need to do is configure the IOC.cs file. In here we simply create maps from our interfaces to our concrete types. I've only gone over a very basic example. What happens when you want to inject a concrete class which takes in parameters in the constructor.