Unit Test and Mock ServiceFilter Attribute of Controllers

Mock ServiceFilter

Today in this article we shall see how to Unit Test and Mock ServiceFilter Attributes of Controllers and write better unit test cases for a controller with a service filter attribute.

Unit test cases of controller-specific methods make API robust and help identify any breaking changes early.

We shall use below sample code below where the controller is attributed with filter attribute i.e ServiceFilter attribute as shown below example,

Unit Test and Mock ServiceFilter Attribute of Controllers

The controller with action filters includes the below-listed methods. These methods wrap the filters that run for a given action,

  • OnActionExecuting – runs before any of the action’s filters.
  • OnActionExecuted – runs after all of the action’s filters.
  • OnActionExecutionAsync– async format

Below is the actual implementation of the ValidateInputAttribute,

Unit Test and Mock ServiceFilter Attribute of Controllers

Lets now create the Unit test case for this action filter attribute which of type ServiceFilter

Step 1- Create a Mock of ActionContext

Below is a simple way of creating a mock of ActionContext. This mock object will be needed in the next steps where we will create a mock setup for ActionExecutingContext.

Below we are creating a mock object of HttpContext, RouteData, and ActionDescriptor using the moq library.

 var actionContext = new ActionContext(
                Mock.Of<HttpContext>(),
                Mock.Of<RouteData>(),
                Mock.Of<ActionDescriptor>(),
                Mock.Of<ModelStateDictionary>()
            );

Step 2- Create a Mock of ActionExecutingContext

Let’s now create the mock for ActionExecutingContext using the mock action context object as defined above. We are also passing a mock list of IFilterMetadata and Dictionary key-value pairs.

var actionExecutingContext = new ActionExecutingContext(
                actionContext,
                new List<IFilterMetadata>(),
                new Dictionary<string, object>(),
                Mock.Of<Controller>()
            );

Step 3: Define the action

Let’s define the action by creating the instance of ValidateInputAttribute and executing the OnActionExecuting method,

 var valInputObject = new ValidateInputAttribute <string>();
 valInputObject.OnActionExecuting(actionExecutingContext);

Step 4: Define Assert

We are making sure the result of validation fails reports BadRequestObjectResult as shown below,

Assert.IsType<BadRequestObjectResult>(actionExecutingContext.Result);

Useful References

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *