title: Unit Testing with IMongoQueryable
body: <p>I am using .NET Core 2.0 and the .NET Core MongoDB driver.</p>
  
  <p>I have created a repository like so:</p>
  
  <pre><code>public interface IRepository&lt;T&gt;
  {
      IMongoQueryable&lt;T&gt; Get()
  }
  </code></pre>
  
  <p>I have done this to give flexibility to whoever uses this to be able to do LINQ much like they would do using EF. The problem is when it comes to unit testing and I'm trying to create an in-memory database so I can check states before and after operation. </p>
  
  <p>Some stuff I tried:</p>
  
  <pre><code>public class InMemoryRepository : IRepository&lt;ConcreteType&gt;
  {
      private HashSet&lt;ConcreteType&gt; _data = new HashSet&lt;ConcreteType&gt;();
  
      public IMongoQueryable&lt;ConcreteType&gt; Get()
      {
          return (IMongoQueryable&lt;ConcreteType&gt;)_data.AsQueryable();
      }
  }
  </code></pre>
  
  <p>The case doesn't work as the interface for <code>IMongoQueryable</code> is:</p>
  
  <pre><code>public interface IMongoQueryable&lt;T&gt; : IMongoQueryable, IQueryable, IEnumerable, IQueryable&lt;T&gt;, IEnumerable&lt;T&gt;, IAsyncCursorSource&lt;T&gt;
  </code></pre>
  
  <p>Another go:</p>
  
  <pre><code>public class InMemoryRepository : IRepository&lt;ConcreteType&gt;
  {
      private HashSet&lt;ConcreteType&gt; _data = new HashSet&lt;ConcreteType&gt;();
  
      public InMemoryRepository()
      {
          _mongoQueryableMock = new Mock&lt;IMongoQueryable&lt;ConcreteType&gt;&gt;();
          _mongoQueryableMock.Setup(m =&gt; m.AsQueryable()).Returns(_data.AsQueryable);
      }
  
      public IMongoQueryable&lt;ConcreteType&gt; Get()
      {
          return _mongoQueryableMock.Object;
      }
  }
  </code></pre>
  
  <p>This doesn't work as <code>IMongoQueryable.AsQueryable()</code> is an extension method and I can't mock/setup that.</p>