title: How do I unit test Windows Azure Table query with a stub using Moq?
body: <p>I can't get my unit test to work properly.</p>
  <p>It works in a integration test I have where it will actually hit the Azure Table Storage.</p>
  <p>The problem I guess is the mocking of the property <strong>QueryableEntities</strong> which returns a <code>Queryable&lt;Word&gt;</code> from the mock but it returns a <strong><a href="http://msdn.microsoft.com/en-us/library/Cc646677%28v=vs.100%29.aspx" rel="nofollow noreferrer">DataServiceQuery</a></strong> from the ServiceContext class. Is it possible to create a stub of type <strong>DataServiceQuery</strong> which returns a Queryable?</p>
  <p>This is my code:</p>
  <p><strong>Test</strong></p>
  <pre><code>[TestMethod]
      public void GetAExistingWordInStorageShouldReturnCorrectWord()
      {
  
          Word expected = new Word(Dictionaries.Swedish.ToString(), &quot;Word&quot;, &quot;Word&quot;);
  
          List&lt;Word&gt; Words = new List&lt;Word&gt;();
          Words.Add(new Word(Dictionaries.Swedish.ToString(), &quot;Word&quot;, &quot;Word&quot;));
  
          IQueryable&lt;Word&gt; WordQueryable = Words.AsQueryable&lt;Word&gt;();
          
          var mock = new Mock&lt;IServiceContext&lt;Word&gt;&gt;();
          mock.Setup(x =&gt; x.QueryableEntities).Returns(WordQueryable);
  
          DictionaryRepository dr = new DictionaryRepository(Models.Dictionaries.Swedish, &quot;testdictionaries&quot;);
          dr.Context = mock.Object;
  
          Word result = dr.GetWord(expected.Text, false);
  
          Assert.AreEqual(expected, result);
      }
  </code></pre>
  <p><strong>IServiceContect interface</strong></p>
  <pre><code>public interface IServiceContext&lt;TEntity&gt;
  {
      IQueryable&lt;TEntity&gt; QueryableEntities {get;}
  }
  </code></pre>
  <p><strong>ServiceContext Class</strong></p>
  <pre><code>public class ServiceContext&lt;TEntity&gt; : TableServiceContext, IServiceContext&lt;TEntity&gt; where TEntity : TableServiceEntity
  {
  
      private readonly string tableName;
  
      public ServiceContext(CloudStorageAccount account, String tableName)
          : base(account.TableEndpoint.ToString(), account.Credentials)
      {
          this.tableName = tableName;
          this.IgnoreResourceNotFoundException = true;
      }
  
      public IQueryable&lt;TEntity&gt; QueryableEntities
      {
          get
          {
              return CreateQuery&lt;TEntity&gt;(tableName);
          }
      }
  
  }
  </code></pre>
  <p><strong>Dictionary Repository</strong></p>
  <pre><code>     public class DictionaryRepository : IDictionaryRepository
  {
      public Dictionaries Dictionary { get; set; }
      public String TableName;
  
      public IServiceContext&lt;Word&gt; Context;
  
      public DictionaryRepository(Dictionaries dictionary)
          : this(dictionary, &quot;dictionaries&quot;)
      {
      }
  
      public DictionaryRepository(Dictionaries dictionary, String tableName)
      {
          Dictionary = dictionary;
          this.TableName = tableName;
          CloudStorageAccount account = CloudStorageAccount.Parse(***);
          Context = new ServiceContext&lt;Word&gt;(account, this.TableName);
      }
  
      public List&lt;Tile&gt; GetValidTiles()
      {
          throw new NotImplementedException();
      }
  
      public Type ResolveEntityType(String name)
      {
          return typeof(Word);
      }
  
      public Word GetWord(string word, Boolean useCache = false)
      {
        
          var q = this.Context.QueryableEntities.Where(x =&gt; x.PartitionKey == Dictionary.ToString() &amp;&amp; x.RowKey == word).AsTableServiceQuery();
  
          Word result = q.Execute().SingleOrDefault();
  
          if (result == null)
              return null;
  
          return result;
  
      }} 
  </code></pre>
  <p><strong>I'm getting the following error</strong></p>
  <p>Error:</p>
  <pre><code>    ArgumentNullException was unhandled by user code
      Value cannot be null.
      Parameter name: query
  </code></pre>
  <p>I get the error when calling <strong>.AsTableServiceQuery()</strong> on the following line in DictionaryRepository class:</p>
  <pre><code>var q = this.Context.QueryableEntities.Where(x =&gt; x.PartitionKey == Dictionary.ToString() &amp;&amp; x.RowKey == word).AsTableServiceQuery();
  </code></pre>