Microsoft Fakes
Shim Async Task
Unit Testing
Mocking Frameworks
.NET Development

How to use Microsoft Fakes to Shim Async Task method?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Microsoft Fakes is a powerful isolation framework in Visual Studio, designed to help developers test their .NET applications by creating mock implementations of methods and classes. One of its core features is the ability to "Shim" code, which allows you to redirect calls from existing methods to mock methods. This feature is particularly useful when dealing with static, sealed, or non-virtual methods, as well as async task methods, allowing you to effectively isolate the code under test.

In this article, we venture into how to use Microsoft Fakes to shim async task methods, an essential capability when testing asynchronous code.

Understanding Shims

Shims are runtime method replacements used in unit testing. They modify the compiled code's Intermediate Language (IL) to redirect calls to specified methods. This redirection allows developers to simulate different conditions and control method behaviors, which can be particularly useful when those methods are dependent on external resources like databases, file systems, or network services.

Context of Async Task Methods

Async task methods in .NET are an integral part of asynchronous programming, enabling developers to perform non-blocking operations while awaiting the completion of potentially time-consuming tasks. Although they are beneficial in application performance, they pose unique challenges in testing due to their asynchronous nature.

Shimming async task methods requires ensuring that the method redirection accurately simulates asynchronous behavior.

Setting Up Microsoft Fakes

Prerequisites

  • Visual Studio Enterprise/Professional: Microsoft Fakes are only available in these editions.
  • .NET Application: The application must be built using the .NET Framework.
  • Async Methods: At least one async task method is required for shimming.

Creating a Fakes Assembly

  1. Add a Fakes Assembly: Right-click on the project references in your test project and select "Add Fakes Assembly" for the assembly containing the method you wish to shim. This action generates a .fakes file in your project.
  2. Modify the .fakes File (Optional): Customize the .fakes file to control the behavior of the shim. This step includes specifying which methods you want to shim or stub, and configuring shimming behaviors.

Shimming an Async Method

To demonstrate shimming an async task method, let’s consider a scenario where your code calls an external service asynchronously.

Example Method

Suppose you have an HttpClient method that fetches data asynchronously:

  • ShimsContext: A context that ensures shims are applied within its scope, handling the application’s control flow.
  • Task.FromResult: A utility method used here to create a completed task with a specified result, simulating the async behavior.
  • Thread Safety: Carefully manage the flow and context around async execution, as async tasks can disrupt typical execution flows.
  • Behavior Complexity: Complex interactions or tasks may require detailed shim setups, involving multiple methods.
  • Testing Reality: Shimming might bypass real code execution, potentially overlooking true bugs.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.