public CloudQueueFactoryTest() { // Note: the tests here heavily depend on "real" Azure.Storage entities - we do mock the virtual methods, // but all constructors are "real" code. // Therefore, a change in next version of Azure.Storage SDK may break our tests (in that case maybe wrap any Azure.Storage entity // in a proxy or a wrapper class with the public interface and test that) _cloudQueueClientFactoryMock = Substitute.For <ICloudQueueClientFactory>(); // Mock entire CloudQueueClient - the factory code for it havily depends on MS Azure.Storage entities // whose logic we do not want to test // What we do want to test is how many times .GetCloudQueueClient() or .GetQueueReference() has been called { _cloudQueueClientMock = Substitute.ForPartsOf <CloudQueueClient>( new StorageUri(new Uri("https://test.com")), new Microsoft.Azure.Storage.Auth.StorageCredentials(), null); _cloudQueueClientMock.When(c => c.GetQueueReference(Arg.Any <string>())).DoNotCallBase(); _cloudQueueClientMock.GetQueueReference(Arg.Any <string>()) .Returns(x => { CloudQueue queue = Substitute.ForPartsOf <CloudQueue>(new Uri($"http://test.com/{x.Arg<string>()}")); queue.When(q => q.CreateIfNotExistsAsync()).DoNotCallBase(); queue.CreateIfNotExistsAsync().Returns(Task.FromResult(true)); return(queue); }); _cloudQueueClientFactoryMock.GetCloudQueueClient().Returns(_ => _cloudQueueClientMock); } _cloudQueueFactory = new CloudQueueFactory(_cloudQueueClientFactoryMock); }