title: Use Generics for Return Type and Parameter
body: <p>I am writing a lot of unit tests and due to the specifics of our framework, each one needs this mock which uses <code>DbSet</code> and some type. The only difference is the type, so I would like to avoid repeating the same method many times and if possible have one (maybe) generic method.
  Using EF 6, .Net 4.7.1</p>
  
  <p>Here is what I have now:</p>
  
  <pre><code>private Mock&lt;DbSet&lt;MyClass&gt;&gt; MockSetupForDbSet(List&lt;MyClass&gt; expected)
  
      {
  
          var mockDbSet = new Mock&lt;DbSet&lt;MyClass&gt;&gt;();
  
  
  
          mockDbSet.As&lt;IQueryable&lt;MyClass&gt;&gt;()
  
              .Setup(m =&gt; m.Provider)
  
              .Returns(new TestDbAsyncQueryProvider&lt;MyClass&gt;(expected.AsQueryable().Provider));
  
  
  
          mockDbSet.As&lt;IQueryable&lt;MyClass&gt;&gt;().Setup(m =&gt; m.Expression).Returns(expected.AsQueryable().Expression);
  
  
  
          return mockDbSet;
  
      }
  </code></pre>
  
  <p>And this is what I need:</p>
  
  <pre><code>private Mock&lt;DbSet&lt;T&gt;&gt; MockSetupForDbSet(List&lt;T&gt; expected) where T : class
  
      {
  
          var mockDbSet = new Mock&lt;DbSet&lt;T&gt;&gt;();
  
  
  
          mockDbSet.As&lt;IQueryable&lt;T&gt;&gt;()
  
              .Setup(m =&gt; m.Provider)
  
              .Returns(new TestDbAsyncQueryProvider&lt;T&gt;(expected.AsQueryable().Provider));
  
  
  
          mockDbSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.Expression).Returns(expected.AsQueryable().Expression);
  
  
  
          return mockDbSet;
  
      }
  </code></pre>
  
  <p>Is this the proper way or should I use another approach, e.g. <code>dynamic</code> parameter?</p>
  
  <p>----- EDIT -----</p>
  
  <p>Here is the final version of the generic method, thanks to the help from StackOverflow members (in case it helps someone in future):</p>
  
  <pre><code>internal static Mock&lt;DbSet&lt;T&gt;&gt; MockSetupForDbSet&lt;T&gt;(List&lt;T&gt; expected) where T : class
  
          {
  
              var mockDbSet = new Mock&lt;DbSet&lt;T&gt;&gt;();
  
  
  
              mockDbSet.As&lt;IQueryable&lt;T&gt;&gt;()
  
                  .Setup(m =&gt; m.Provider)
  
                  .Returns(new TestDbAsyncQueryProvider&lt;T&gt;(expected.AsQueryable().Provider));
  
  
  
              mockDbSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(expected.AsQueryable().ElementType);
  
  
  
              mockDbSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(expected.AsQueryable().GetEnumerator());
  
  
  
              mockDbSet.As&lt;IDbAsyncEnumerable&lt;T&gt;&gt;()
  
                  .Setup(m =&gt; m.GetAsyncEnumerator())
  
                  .Returns(new TestDbAsyncEnumerator&lt;T&gt;(expected.GetEnumerator()));
  
  
  
              mockDbSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.Expression).Returns(expected.AsQueryable().Expression);
  
  
  
              return mockDbSet;
  
          }
  </code></pre>
  
  <p>The test is written based and depends on Microsoft documentation 'Testing with a mocking framework":
  <a href="https://docs.microsoft.com/en-us/ef/ef6/fundamentals/testing/mocking#async" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/ef/ef6/fundamentals/testing/mocking#async</a></p>