title: How to write this EF Mock setup code as a reusable Generic Boilerplate?
body: <p>I am using moq, ef 6 and xunit. I find myself writing this code over and over and thought maybe I could make it into a generic method but having some trouble.</p>
  
  <pre><code>public static void CreateSalesMock(List&lt;Sale&gt; sales, Mock&lt;DatabaseContext&gt; dbContextMock)
  {
      var data = sales.AsQueryable();
  
      var mockSet = new Mock&lt;DbSet&lt;Sale&gt;&gt;();
      mockSet.As&lt;IQueryable&lt;Sale&gt;&gt;()
             .Setup(x =&gt; x.Provider)
             .Returns(data.Provider);
      mockSet.As&lt;IQueryable&lt;Sale&gt;&gt;()
             .Setup(x =&gt; x.Expression)
             .Returns(data.Expression);
      mockSet.As&lt;IQueryable&lt;Sale&gt;&gt;()
             .Setup(x =&gt; x.ElementType)
             .Returns(data.ElementType);
      mockSet.As&lt;IQueryable&lt;Sale&gt;&gt;()
             .Setup(x =&gt; x.GetEnumerator())
             .Returns(data.GetEnumerator());
  
      dbContextMock.Setup(x =&gt; x.Sales).Returns(mockSet.Object);
  }
  </code></pre>
  
  <p>Now I have many other tables in my database so if I could write one method that would take in a list of that data and set it up so I can mock query through it would be great.</p>
  
  <pre><code>public static void CreateMockSet&lt;T, TA, TB&gt;(T dataList, TA model, 
      Func&lt;TB&gt; lambda, Mock&lt;DatabaseContext&gt; dbContextMock) 
       where T : List&lt;T&gt;
       where TA: Mock&lt;DbSet&lt;TA&gt;&gt;
  {
      var data = dataList.AsQueryable();
  
      model.As&lt;IQueryable&lt;T&gt;&gt;()
           .Setup(x =&gt; x.Provider)
           .Returns(data.Provider);
      model.As&lt;IQueryable&lt;T&gt;&gt;()
           .Setup(x =&gt; x.Expression)
           .Returns(data.Expression);
      model.As&lt;IQueryable&lt;T&gt;&gt;()
           .Setup(x =&gt; x.ElementType)
           .Returns(data.ElementType);
      model.As&lt;IQueryable&lt;T&gt;&gt;()
           .Setup(x =&gt; x.GetEnumerator())
           .Returns(data.GetEnumerator());
  
      dbContextMock.Setup(x =&gt; lambda);
  }
  </code></pre>
  
  <p>So far I have that but I am not sure if that will work or not. I am stuck passing in the "lambda" part(<code>x =&gt; x.Sales</code> for example) so I can't even test it. </p>