Unit Testing Filters in ASP.NET Web API

Unit Testing Filters in ASP.NET Web API


12 Articles


In this article, let's learn about how to unit test Filters in WebAPI in ASP.NET Core.

Table of Contents

  1. Introduction
  2. Why Unit Testing?
  3. What is a Filter?
  4. Types of Filter
  5. Unit Testing Action Filter
  6. Summary


ASP.NET Core filters play a crucial role in the request processing pipeline, allowing code to run before or after specific stages. Filters are utilized to handle cross-cutting concerns, such as authorization, error handling, and caching. Creating custom filters is a common practice to implement unique behaviors and avoid code duplication. Since filters introduce custom behavior, it is essential to test them to ensure their correctness and reliability. In this article, we will focus on unit testing an action filter in ASP.NET Core, exploring the importance of unit testing and providing practical examples for better understanding.

Why Unit Testing ?

Unit testing is a fundamental practice in software development that involves testing individual components or units of code in isolation to validate their functionality. The benefits of unit testing include:

  • Early bug detection
  • Improved code quality
  • Refactoring safety net
  • Documentation
  • Confidence in code changes

Note: If you have not done so already, I recommend you read the article on Implementing TDD in C# .Net.

What is a Filter ?

In the context of ASP.NET Core, filters are components that execute before or after specific stages in the request processing pipeline. ASP.NET Core provides various built-in filters, such as authorization and response caching filters, and allows developers to create custom filters to address specific requirements.

Types of Filter

The different types of filters in ASP.NET Core are:

  • Action Filters: Run immediately before and after an action method is invoked. They can modify the arguments passed to an action and alter the result returned by an action.
  • Authorization Filters: Control access to actions or resources based on user credentials or other criteria.
  • Resource Filters: Execute before and after model binding and action execution. They are useful for performing tasks like logging or caching.
  • Exception Filters: Handle exceptions thrown during the execution of an action and provide custom error handling logic.
  • Result Filters: Operate on the result generated by an action before it's sent back to the client.

Unit Testing Action Filter

It would lead us too far to get into all the details on the different types of filters and when exactly they run. Instead, we're going to choose one type of filter, an action filter. Those run immediately before and after an action method is called. They can change the arguments passed into an action, and they can change the result returned from an action. Other filter types can be tested in a likewise manner as action filters, so what you will learn in the upcoming demo can be applied to those other types of filters as well. Important is to know how to mock the dependencies. Let's check that out with a demo.

Code Sample - WebAPI Action Filter

From the above code we can see that we have a simple ActionFilter named ValidateUserRole used to return BadRequestResult if Invoked with User not having master role Claim.

Now let's add test to this filter. The steps are,

  1. In Arrange, the first step is to new() up ValidateUserRole ActionFilter
  2. The ActionFilter needs ActionExecutingContext. To create that we need ActionContext which in turn depend on HttpContext. We need to properly mock / create them.
  3. For HttpContext, we can simply use the DefaultHttpContext.
  4. We can use the above created HttpContext and use it as a first parameter to create ActionContext and simply pass new() to rest of parameters.
  5. Now we can create ActionExecutingContext by passing the ActionContext, an empty list of FilterMetadata, an empty dictionary, and a null controller instance.
  6. The Act step involves executing the OnActionExecuting method of the filter, passing the previously arranged ActionExecutingContext as the parameter.
  7. Finally in the Assert step, we verify the expected behavior of the action filter. In our example, we assert that the result returned by the ActionExecutingContext is of type BadRequestResult, indicating that a bad request should be returned.
  8. You can perform this assertion by checking the type of the result as Assert.IsType<BadRequestResult>(actionExecutingContext.Result);

Code Sample - WebAPI Action Filter Test


Unit testing ASP.NET Core filters is crucial to ensure the correctness and reliability of custom behaviors implemented in your application. By performing unit tests on filters, you can catch bugs early, improve code quality, and gain confidence in making code changes and enhancements.

In this article, we focused on unit testing an action filter in ASP.NET Core. We explored the importance of unit testing and discussed the different types of filters available. Using the example of the ValidateUserRole filter, we walked through the steps of arranging, acting, and asserting to effectively test the filter's behavior.

Remember, unit testing is an ongoing process that should be integrated into your development workflow. By investing time in creating comprehensive unit tests, you can build robust and maintainable ASP.NET Core applications that provide reliable and expected results.

Keep exploring the various types of filters and their testing approaches to enhance your understanding and proficiency in ASP.NET Core development. Happy coding and testing!

  • Webapi
  • Unit Test
  • Filters