asp.net-mvcunit-testingglobal-filter

Unit test Global filters in MVC


I have a MVC4 application and adding bunch of global filters in global.asax. They need to be in one order. I want to write a unit test to make sure they are always in the desired order. Is there a way to access all global filters or test this?


Solution

  • You register the global filters in FilterConfig by default (under App_Start), right? This is something like this:

    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
    }
    

    You can then call this method from a test case and pass a GlobalFilterCollection instance and after that you can test the order of the GlobalFilterCollection you passed.