title: Mocking CloudStorageAccount and CloudTable for Azure table storage
body: <p>So I am trying to test Azure Table Storage and mock things that I depend on. My class is structured in a way that I establish a connection in the constructor, i.e. I create a new instance of <code>CloudStorageAccount</code> in which I create an instance of <code>StorageCredentials</code> that has <code>storageName</code> and <code>storageKey</code>. Afterwards, I create an instance of <code>CloudTable</code>, which I use further in the code to perform CRUD operations. My class looks as follows:</p>
  <pre><code>public class AzureTableStorageService : ITableStorage
  {
          private const string _records = &quot;myTable&quot;;
          private CloudStorageAccount _storageAccount;
          private CloudTable _table;
  
          public AzureTableStorageService()
          {
              _storageAccount = new CloudStorageAccount(new StorageCredentials(
                   ConfigurationManager.azureTableStorageName, ConfigurationManager.azureTableStorageKey), true);
              _table = _storageAccount.CreateCloudTableClient().GetTableReference(_records);
              _table.CreateIfNotExistsAsync();
          }
  
          //...
          //Other methods here
  }
  </code></pre>
  <p><code>_table</code> is reused throughout the class for different purposes. My goal is to mock it, but since it is virtual and doesn't implement any interface, I can't come over with a simple <code>Mock</code> solution like:</p>
  <pre><code>_storageAccount = new Mock&lt;CloudStorageAccount&gt;(new Mock&lt;StorageCredentials&gt;((&quot;dummy&quot;, &quot;dummy&quot;), true));
  _table  = new Mock&lt;CloudTable&gt;(_storageAccount.Object.CreateCloudTableClient().GetTableReference(_records));
  </code></pre>
  <p>Therefore, when I try to construct my Unit Test in this way I am getting:
  <code>Type to mock must be an interface or an abstract or non-sealed class.</code></p>
  <p>My goal is to accomplish something like:</p>
  <pre><code>_table.Setup(x =&gt; x.DoSomething()).ReturnsAsync(&quot;My desired result&quot;);
  </code></pre>
  <p>Any ideas are highly appreciated!</p>