title: Moq - How to implement mock for method of generic base class?
body: <p>I have the following interfaces and service implemented like this:</p>
  <pre><code>public interface IParentInterface&lt;T&gt; where T : class
  {
      T GetParent(T something);
  }
  
  public interface IChildInterface : IParentInterface&lt;string&gt;
  {
      string GetChild();
  }
  
  public class MyService
  {
      private readonly IChildInterface _childInterface;
  
      public MyService(IChildInterface childInterface)
      {
          _childInterface = childInterface;
      }
  
      public string DoWork()
      {
          return _childInterface.GetParent(&quot;it works&quot;);
      }
  }
  </code></pre>
  <p>Here is my test for the <strong>DoWork</strong> method of <strong>MyService</strong> class:</p>
  <ul>
  <li>Create a new mock object of the child interface</li>
  <li>Setup the GetParent method of the parent interface</li>
  <li>Pass the mock object to the service constructor</li>
  <li>Execute the DoWork</li>
  <li>Expect to have the <strong>resp = &quot;It really works with something!&quot; but it's null</strong></li>
  </ul>
  <pre><code>[Fact]
  public void Test1()
  {
      var mockInterface = new Mock&lt;IChildInterface&gt;();
      mockInterface
          .As&lt;IParentInterface&lt;string&gt;&gt;()
          .Setup(r =&gt; r.GetParent(&quot;something&quot;))
          .Returns(&quot;It really works with something!&quot;);
  
      var service = new MyService(mockInterface.Object);
  
      string resp = service.DoWork(); // expects resp = &quot;It really works with something!&quot; but it's null
  
      Assert.NotNull(resp);
  }
  </code></pre>
  <p>Other info:</p>
  <ul>
  <li>Moq 4.16.1</li>
  <li>.NET CORE (.NET 5)</li>
  <li>XUnit 2.4.1</li>
  </ul>