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 . . .
UPDATE: Since putting this article together MS have released their VS2010 and MSDN Licensing White Paper, which whilst it confirms all of the following should be used in preference to this information. Original Article: Firstly, a bit of a disclaimer – the following are just my observations/opinions only and should not be used to plan any purchase or decision. I also have no association with Microsoft or any reseller. As part of a new role . . .
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 / . . .
When unit testing ASP.NET MVC2 projects the issue of injecting HttpContext is quickly encountered. There seem to be many different ways/recommendations for mocking HttpContextBase to improve the testability of controllers and their actions. My investigations into that will probably be a separate blog post in the near future but for now, I want to cover something that had me stuck for longer than it probably should have. That is how to mock non-abstract/interfaced classes within . . .