title: How to (should I) mock DocumentClient for DocumentDb unit testing?
body: <p>From the new CosmosDb emulator I got sort of a repository to perform basic documentdb operations, this repository gets injected to other classes. I wanted to unit test a basic query. </p>
  
  <pre><code>public class DocumentDBRepository&lt;T&gt; where T : class
  {
   //Details ommited...
  
      public IQueryable&lt;T&gt; GetQueryable()
      {
          return _client.CreateDocumentQuery&lt;T&gt;(
              UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId),
              new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true });
      }
  
      public async Task&lt;IEnumerable&lt;T&gt;&gt; ExecuteQueryAsync(IQueryable&lt;T&gt; query)
      {
          IDocumentQuery&lt;T&gt; documentQuery = query.AsDocumentQuery();
          List&lt;T&gt; results = new List&lt;T&gt;();
          while (documentQuery.HasMoreResults)
          {
              results.AddRange(await documentQuery.ExecuteNextAsync&lt;T&gt;());
          }
  
          return results;
      }
  
  
  }
  </code></pre>
  
  <p>This repository needs a document client to work, which also gets injected on the constructor. </p>
  
  <pre><code>public DocumentDBRepository(string databaseId, IDocumentClient client)
  {
      _client = client;
      _databaseId = databaseId;
      _collectionId = GetCollectionName();
  }
  </code></pre>
  
  <p>My initial approach was to use the CosmosDb emulator, but that required the emulator to run which I don't like and makes the tests slower. </p>
  
  <p>My second approach was to try and use a mock of the document client. </p>
  
  <pre><code>var data = new List&lt;MyDocumentClass&gt;
  {
      new MyDocumentClass{ Description= "BBB" },
      new MyDocumentClass{ Description= "ZZZ" },
  
  }
  .AsQueryable()
  .OrderBy(q =&gt; q.Description);
  var client = new Mock&lt;IDocumentClient&gt;();
  client.As&lt;IDocumentClient&gt;()
      .Setup(foo =&gt; foo.CreateDocumentQuery&lt;MyDocumentClass&gt;(It.IsAny&lt;Uri&gt;(), It.IsAny&lt;FeedOptions&gt;()))
      .Returns(data);
  
  DocumentDBRepository&lt;MyDocumentClass&gt; repo= new DocumentDBRepository&lt;MyDocumentClass&gt;(cosmosDatabase, client.Object);
  </code></pre>
  
  <p>The code that uses this repository works like this:</p>
  
  <pre><code>var query = _documentsRepository.GetQueryable()
                  .Where(d =&gt; d.Description = description)
                  .OrderByDescending(d =&gt; d.description)
                  .Take(100);
  //Execute query async fails. 
  var result = await _documentsRepository.ExecuteQueryAsync(query);
  </code></pre>
  
  <p>It fails because the repository tries to convert the <code>IQueryable</code> to a <code>IDocumentQuery</code> object, which is very specific to DocumentDb api (See method <code>ExecuteQueryAsync</code> above). Later on, it executes <code>HasMoreResults</code> method on it. So the problem is, even if I mock <code>.asDocumentQuery()</code> to return my object, I don't know how to provide a result for <code>HasMoreResults</code> and <code>ExecuteNextAsync</code> so that it makes sense for my unit tests. </p>
  
  <p>My third option would be to straight mock my repository instead of the DocumentClient object. Would be, I think, simpler, but I wouldn't be testing much of the DocumentDb api. </p>