Recently we’ve been looking at improving our unit test code coverage and reducing the amount of duplicated code around our bespoke data access layer. Where possible we have moved over to NHibernate but certain parts of the data access must still be written using the standard ADO.NET connection/command pattern. Typically hidden right in the middle of this bespoke code is a while loop that is pivoting a data reader into a POCO that is impossible . . .
By default objects mocked using MOQ will handle calls to method/properties that have not been defined, which 99% of the time will be the desired behaviour. Occasionally however a situation will require that MOQ throws an exception when accessing an undefined method or property. The MOQ framework provides this functionality via the MockBehavior enum but this can only be set during the construction of the mocked object. Typically this won’t cause any problems as the . . .
I recently asked this question on Stack Overflow. I was using reflection and MethodInfo to confirm that a controller contained an action with the required method parameters. If one was found, this would then allow my code to unit test that the correct Action Filter Attributes were being assigned to the controller action (so a particular action could only respond to an HTTP GET or POST as an example). This was all working fantastically well . . .
It is a shame that when the ASP.NET MVC framework was released they did not think to build IoC support into the infrastructure. All the major components of the MVC engine appear to magically inherit instances of HttpContext and it’s related objects – which can cause no end of problems if you are trying to utilise Unit Testing and IoC. Reading around various articles on the subject just to get around this one problem requires . . .
The code below defines an object that holds an instance of the Castle Windsor container and sets it up to handle all requests to resolve ASP.NET MVC Controllers. It also includes code to inject instances of HttpRequest and HttpContextBase. This means that you can define injected objects that contain references to HttpContextBase in their constructors (which can be your MVC controllers) and they will receive populated instances of these objects. It is worth noting that . . .
As I’ve previously mentioned anyone starting out with IoC and ASP.NET MVC quickly encounters problems injecting HttpContext and related classes into controllers, etc. A similar issue surrounds Action Filter Attributes but is not limited to just HttpContext as objects inheriting from ActionFilterAttribute must contain a “parameterless” default constructor. Without a “parameterless” constructor these objects can not be created when used to decorate a class or action declaration. Also, the MVC engine is responsible for the . . .
Having recently taken a look at NServiceBus, the first obstacle that I encountered was that it was not compatible with the version of Castle Windsor which we were using. Luckily due to the design of NServiceBus adding a new version of an IoC framework is relatively painless if you follow a couple of basic steps and follow the format laid out by the existing framework. Whilst the NServiceBus documentation say what you need to do, . . .
To fully test an MVC web site it is important to test (in isolation) the following: The behaviour of controller actions The behaviour of any custom action filters. The decoration of action filter attributes on controller actions. To test the 3rd point, you must use reflection to select the desired action from the controller. The following method takes an action name and a Tuple array of “Type” and “String”. Used together this combination should be . . .
Update – 14th February 2016:Looking in my blog stats, this continues to be one of my most popular articles, so it is most definitely worth an update. As of v4.2.0 Automapper has been updated to remove the static implementation. I’ve not had chance to play with the new version yet but I would imagine this version will now work with any IoC container you wish to use it with. Original Article:The main “Mapper” class of . . .
In my current role, we have come across a requirement to map message objects to more complex domain entities. As anyone that has done this previously will quickly tell you, manually writing code to do this is very boring and repetitive. With very limited time and resources, that effort could be better spent elsewhere on the project. As it seemed very simple to set up and start using we’ve decided to use AutoMapper. Configuring / . . .