Unit Testing ASP.NET MVC Controller Action contains specific ActionFilter Attributes
ActionFilters in ASP.NET MVC are great.
You can now easily share logic between controllers without having to inherit from a base controller, that does the common work.
I have a content heavy application that supports a set of layouts. Each layout is rendered by setting the Layout view dynamically, which I do from an ActionFilter. My ActionFilter is fully tested, but when you delegate work to an ActionFilter, you should write a test that ensures the filter is defined on the action.
Not surprisingly, this is very simple and relies on reflection (Download from Gist: https://gist.github.com/2605628):
///
And the usage looks like this:
[TestMethod] public void Index_ContainsTemplateLayoutAttribute() { HomeController controller = new HomeController(); bool containsAttribute = controller.VerifyControllerActionAttribute(controller.Index, typeof(TemplateLayoutAttribute)); Assert.IsTrue(containsAttribute); }
Need an introduction? Watch a video about unit testing ASP.NET and MVC based web apps on Vimeo, published by Typemock.