Skip to main content

Posts

Showing posts from May, 2011

Unit Testing C# attributes

For a recent coding session I needed to handle an exception being thrown when some Json was incorrectly bound to a view model. With the framework we were using (ASP.NET MVC2) I was unable to handle the exception at the controller level, nor could I handle it at the "global" level when the framework carries out its bindings. Another way ASP.NET MVC handles exceptions is via attributes to catch errors you specify. The resulting exception is strongly typed and then can be passed into a view, from which you have full control of what to do. Typically we would log the error, display a friendly message and so forth.In the past these attributes have been simply applied without a test - the general consensus being this was a framework specific thing which had no value in being tested. I agreed with those statements up until several minutes ago. Having fixed a defect in which the user was not seeing a friendly error message I carr…