Monday, September 5, 2016

Unit Testing Custom ASP.NET MVC Validation Attributes

When you are using custom ASP.NET MVC Validation Attributes, you may encounter a problem when attempting to Unit Test this functionality since inheriting from the ValidationAttribute class may require a method such as the following:

This works just fine when testing the public implementation of IsValid, but what if you are implementing the protected method of IsValid? Well, as you will quickly discover, you will not be able to easily Unit Test protected methods:

So how do you work around this limitation?

Well, thanks to this handy article (, you can do something like the following:

Now, when you write your Unit Test, you will do something like this to ACTUALLY Unit Test your Custom Validation Attribute:

No comments:

Post a Comment