The other day I was writing some unit tests for testing my MVC application’s forms authentication classes. I needed to Mock the System.Web.HttpContext object. There are a couple of ways to do this depending on the version of Visual Studio you are using (i.e. Professional, Premium/Ultimate/Enterprise) and how deep you wish to provide some default data. One takes a little more leg work and requires some manual data setting, but gives you greater control. While the other requires less coding for simple basic use.
Do your MVC model properties contain attributes? Have you ever wanted to unit test the properties to verify that the ModelState fails or succeeds based on given values? Below is a static method that can be used for your unit tests as you test your models.
Ever wanted to restrict actions to only responding to Ajax requests? How about restricting them through the use of a custom attribute?
As a Microsoft Certified Trainer over the past 2 years, I’ve had the opportunity of teaching hundreds of professionals how to properly engage in unit testing. Amazingly enough, even though unit testing has been around quite a while, it is still a concept that is very infantile in the corporate arena. Most developers who have adopted unit testing are those who work for start-ups or the few companies who’ve started to embrace agile methodologies executing test-driven development (TDD).
There are times where you may want to display custom validation error messages in MVC. As for me, I’m not a huge fan of the classic ValidationSummary HTML helper. I will still annotate my data model, but I want the errors to have a better presentation in the UI. Occasionally, I may want to display one error at a time. In order to do all of this, there’s a few steps that need to be implemented.
Often times you may want to render a custom validation message that contains HTML markup. If you simply add the line:
This will encode the markup so that less than signs and greater than signs are rendered < and >, respectively. This, of course, is not what you want.
To render your HTML markup, its a little dirty, but it does work.
Simply add the following to your view:
Where “Email” is a property of your model.
When deploying an MVC application to a hosting environment you may receive the error:
[FileNotFoundException: Could not load file or assembly ‘Microsoft.Web.Infrastructure, Version=184.108.40.206, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system cannot find the file specified.]
This is also one of a few component libraries that are needed for deploying an MVC application:
The system libraries are installed with .NET 4, however, ‘Microsoft.Web.Infrastructure.dll’ is only installed when Visual Studio is installed on the machine. Therefore, short of needing to install MVC and Visual Studio on a production environment, we need to deploy the libraries with out application – and we’d like to do so automatically.
There are a few ways to automatically deploy the ‘Microsoft.Web.Infrastructure.dll’ component library with your application. The steps depend on which version of Visual Studio you are using.