I've played with Azure a lot since last year and I can tell you that the Microsoft Azure Management Libraries (MAML) from and Azure SDK are very well designed and that writing tests against code that use them is a charm.

What is Microsoft Fakes?

Microsoft Fakes help you isolate the code you are testing by replacing other parts of the application with stubs or shims. These are small pieces of code that are under the control of your tests. By isolating your code for testing, you know that if the test fails, the cause is there and not somewhere else. Stubs and shims also let you test your code even if other parts of your application are not working yet.

First I created a UnitTest project in Visual Studio. I then added Microsoft.WindowsAzure.Management NuGet package to my unit test project. This will add a lot of other depedencies.

To enable Fakes for our example, select the Microsoft.WindowsAzure.Managemement reference, right-click on it and choose Add Fakes Assembly.


This will add a Fakes folder to your project with a file named Microsoft.WindowsAzure.Management.fakes. This is the xml file you can use to limit which fakes will be generated. If you don't modify it, everything that can be generated will be. I usually scope the generation to only the types I use.

Let's do a unit test as a basic scenario that will prevent the actual http call to Azure but still return a list of fake subscription via a normal SubscriptionClient instance.

Create a ShimsContext

using (var context = ShimsContext.Create()) {
	// Insert your code here

It is within this Disposable object that the magic of Fakes happen.

Create a Stub for ISubscriptionOperations

var fakeSubscriptionId = new Guid("14201e53-1921-4a12-a1f0-c95735706f7a");
var fakeRequestId = new Guid("6a1c1756-fa31-49a5-8c24-f013214692ae");
ISubscriptionOperations subscriptions = new Microsoft.WindowsAzure.Subscriptions.Fakes.StubISubscriptionOperations
    ListAsyncCancellationToken = (cancelationToken) =>
        Task.FromResult(new SubscriptionListOperationResponse
            RequestId = fakeRequestId.ToString(),
            StatusCode = System.Net.HttpStatusCode.OK,
            Subscriptions = new[] {
                new SubscriptionListOperationResponse.Subscription {
                        SubscriptionId = fakeSubscriptionId.ToString(),
                        SubscriptionName = "Fake subscription",
                        SubscriptionStatus = SubscriptionStatus.Active

Override the default behavior for SubscriptionClient.Subscriptions using Shim

Microsoft.WindowsAzure.Subscriptions.Fakes.ShimSubscriptionClient.AllInstances.SubscriptionsGet = (subscriptionClient) => subscriptions;

Now do your normal stuff

// Act:
var results = CloudContext.Clients
  .CreateSubscriptionClient(new AnonymousCloudCredentials())

That's it! This is a basic example of how you can use Microsoft Fakes to alter the behavior of a class, in this example using MAML libraries.

Since everything is very well decoupled and there's an interface for every clients and operations in MAML libraries it is really easy to replace the original behavior with or without Microsoft Fakes.

SampleCode AzureAndFakes.zip
The sample project requires Visual Studio 2013

Learn about Microsoft Fakes