Faking out Azure – Unit Testing with Microsoft Fakes


Microsoft Fakes provides an excellent framework for code isolation within Unit Tests and is only available for Visual Studio Enterprise subscriptions. Fakes does provide advantages over other frameworks (re., Moq and Rhino Mocks) as it allows for full code isolation and not just interfaces or public virtual members.  There are alternatives to Fakes (e.g., Telerik JustMock and TypeMocks).

The MSDN project, Azure Storage and Microsoft Fakes, was created to show some of the cool things Microsoft Fakes provides when isolating Azure Storage.

Please see the project for more information and the following is shown here as an overview.


To illustrate a simple class, StorageManager, was created to send an update a product to an Azure Storage Table called Products.  The entire class is shown below:

namespace MSDNFakeOutStorage
  public class StorageManager
    public bool UpdateProducts(ProductEntity entity)
      var table = GetClientForTable("Products"); 
      var result = table.Execute(TableOperation.InsertOrReplace(entity));
      return result.HttpStatusCode == 204;
    private readonly string ConnectionString = ConfigurationManager.AppSettings["AzureStorageConnectionString"];
    private CloudTable GetClientForTable(string tableName)
      var account = CloudStorageAccount.Parse(ConnectionString);
      var tableClient = account.CreateCloudTableClient();
      var table = tableClient.GetTableReference(tableName);
      return table;

We will create two basic tests.  One to test how the StorageManager behaves when the Products table is successfully updated and when it is not updated successfully.

To start we need to add to fakes to our project: Microsoft.WindowsAzure.Storage and System.Configuration:


The following shows the generated fakes in the project:


Unit Test – System.Configuration

The first step is to isolate the ConfigurationManager.AppSettings from the StorageManager.ConnectionString.  The following will divert all ConfigurationManager.AppSettings[] calls to a new NameValueCollection and is shown below:

ShimConfigurationManager.AppSettingsGet = () =>
  var settings = new NameValueCollection();
  settings.Add("AzureStorageConnectionString", "valueOfAzureStorageConnectionString");
  return settings;

When debugging the unit test you can see the magic working:


Unit Test – Microsoft.WindowsAzure.Storage

The next step is to create a shim for the Azure Storage components used in the project.  A shim was used instead of a stub as we are not replacing the entire object but only diverting messages to our test functionality.

In the code snippet below, note how all calls from the StorageManager class to the underlying storage classes are supplied:

ShimCloudStorageAccount.ParseString = (connectionString) =>
  if (connectionString != "valueOfAzureStorageConnectionString")
    Assert.Fail(string.Format($"Unexpected connection string value of {connectionString} received!"));
  return new ShimCloudStorageAccount
    CreateCloudTableClient = () => new ShimCloudTableClient
      GetTableReferenceString = (tableName) =>
        if (tableName != "myTable")
          Assert.Fail("The table name should be Products");
        return new ShimCloudTable
          CreateIfNotExistsTableRequestOptionsOperationContext = (requestOptions, operationContext) => true,
          ExecuteTableOperationTableRequestOptionsOperationContext = (operation, requestOptions, context) =>
            return new TableResult
              // successful response code is No Content
              // https://msdn.microsoft.com/en-us/library/azure/hh452242.aspx
              HttpStatusCode = 204


This article provides an example of how Microsoft Fakes can isolate Azure Storage from code.  Microsoft Fakes empowers some developers to write more complete unit tests and for others helps to reduce the credibility of using the “It’s too hard” excuse in avoiding to write unit tests.

More Information

Isolating Code Under Test with Microsoft Fakes
Using shims to isolate code
Using stubs to isolate code

This entry was posted in Azure, AzureStorage, C#, Microsoft Fakes, Unit Testing. Bookmark the permalink.

Leave a Reply

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