This one is a little less explored as far as Internet Search goes. How do you mock a Azure Table Storage operations?
I recently ran into this problem and thought of documenting the steps required to easily Shim the Azure Table Storage. I used MSTest and Microsoft Fakes for creating Shim.
ShimCloudStorageAccount.ParseString = s => new ShimCloudStorageAccount();
ShimCloudStorageAccount.AllInstances.CreateCloudTableClient = account => new ShimCloudTableClient();
ShimCloudTableClient.AllInstances.GetTableReferenceString = (client, s) => new ShimCloudTable();
ShimTableResult.Constructor = result => new ShimTableResult();
Now - good thing is that there is nothing to be changed in the code. Your code should work against both actual ATS and Shim ATS.
I recently ran into this problem and thought of documenting the steps required to easily Shim the Azure Table Storage. I used MSTest and Microsoft Fakes for creating Shim.
Once Fake is added, Add Shims for Azure Table Storage related classes. Few parts are highlighted in yellow as those parts let you control what data is queried upon. Once you have bound the observable source to ShimTableQuery, even the dynamic filters applied in your ATS queries get applied on the mock data.
[TestInitialize]
public void Initialize()
{
ShimsContext shimsContext = ShimsContext.Create();
ShimCloudStorageAccount.ParseString = s => new ShimCloudStorageAccount();
ShimCloudStorageAccount.AllInstances.CreateCloudTableClient = account => new ShimCloudTableClient();
ShimCloudTableClient.AllInstances.GetTableReferenceString = (client, s) => new ShimCloudTable();
ShimTableOperation.InsertOrMergeITableEntity = entity => new ShimTableOperation(); // Insert,Update Shim
ShimTableResult.Constructor = result => new ShimTableResult();
ShimTableResult.AllInstances.ResultGet = result => new Entity(); // The entity mapped to the storage.
ShimCloudTable.AllInstances.CreateIfNotExistsAsync = table => Task.FromResult(true);
ShimCloudTable.AllInstances.ExecuteAsyncTableOperation =
(table, operation) => Task.FromResult(new ShimTableResult().Instance);
(table, operation) => Task.FromResult(new ShimTableResult().Instance);
var q = new ShimTableQuery();
q.Bind( entities); // observable collection of entities that you want to reflect in shim table storage.
q.Bind(
ShimCloudTable.AllInstances.CreateQueryOf1<Entity>((table) => q);
}
[TestCleanup]
public void Cleanup()
{
shimsContext?.Dispose();
}
public void Cleanup()
{
shimsContext?.Dispose();
}
Now - good thing is that there is nothing to be changed in the code. Your code should work against both actual ATS and Shim ATS.
No comments:
Post a Comment