title: Mock a method of class under test with Moq & AutoMock
body: <p>I've been reading around and I can't seem to find a case that matches the behavior I want with Moq.
  I want to mock a specific method (which is interfacing with an external API) of a class that I want to test. The problem is that, as I understand it, once the class is <code>.Create&lt;T&gt;</code>, you cannot <code>.Setup()...</code> any method on it.</p>
  <p>Here is an example:</p>
  <pre><code>public class ClassA
  {
     public void ConfigureExpansion()
     {
           var classB = new ClassB();
  
           var data = GetExternalData(); // I want to mock this
           //data is used in the code
  
           classB.TaskB(data); // I want to mock this
     }
  
     public string GetExternalData()
     {
           return data...
     }
  }
  
  public class ClassB
  {
     public void TaskB(String myData)
     {
           //Does some work
     }
  }
  </code></pre>
  <pre><code>[TestMethod]
  public void Test()
  {
     using (var mock = AutoMock.GetLoose())
     {
        mock.Mock&lt;IClassB&gt;().Setup(x =&gt; x.TaskB(It.IsAny&lt;string&gt;()));
                       
  
        var cls = mock.Create&lt;ClassA&gt;();
        cls.Setup(x =&gt; x.GetExternalData(It.IsAny&lt;string&gt;())).Return(&quot;MyData&quot;);   // &lt;--- This is not valid, but represents what I am trying to do.
  
        //act
        cls.ConfigureExpansion();
  
        mock.Mock&lt;IClassB&gt;().Verify(x =&gt; x.TaskB(), Times.Once);
     }
  }
  </code></pre>
  <p>I tried to set up a mock for the interface of my class under test, but the <code>cls</code> won't call it:</p>
  <pre><code>[TestMethod]
  public void Test()
  {
     using (var mock = AutoMock.GetLoose())
     {
        mock.Mock&lt;IClassA&gt;().Setup(x =&gt; x.GetExternalData(It.IsAny&lt;string&gt;())).Return(&quot;MyData&quot;); //&lt;--- Mocking the method here is not detected by the cls below
  
        mock.Mock&lt;IClassB&gt;().Setup(x =&gt; x.TaskB(It.IsAny&lt;string&gt;()));             
  
        var cls = mock.Create&lt;ClassA&gt;();
  
        //act
        cls.ConfigureExpansion();
  
        mock.Mock&lt;IClassB&gt;().Verify(x =&gt; x.TaskB(), Times.Once);
     }
  }
  </code></pre>
  <p>I've also tried instantiating my class-under-test and when I do it like this, it doesn't take the mocks (ClassB) into consideration:</p>
  <pre><code>[TestMethod]
  public void Test()
  {
     using (var mock = AutoMock.GetLoose())
     {
        mock.Mock&lt;IClassA&gt;().Setup(x =&gt; x.GetExternalData(It.IsAny&lt;string&gt;())).Return(&quot;MyData&quot;); //&lt;--- Mocking the method here is not detected by the cls below
  
        mock.Mock&lt;IClassB&gt;().Setup(x =&gt; x.TaskB(It.IsAny&lt;string&gt;()));             
  
        var cls = new ClassA();
  
        //act
        cls.ConfigureExpansion();
  
        mock.Mock&lt;IClassB&gt;().Verify(x =&gt; x.TaskB(), Times.Once);
     }
  }
  </code></pre>
  <p>I would really appreciate any insights about this,
  Thank you</p>