title: When mocking a class with Moq, how can I CallBase for just specific methods?
body: <p>I really appreciate Moq's <code>Loose</code> mocking behaviour that returns default values when no expectations are set. It's convenient and saves me code, and it also acts as a safety measure: dependencies won't get unintentionally called during the unit test (as long as they are virtual).</p>
  
  <p>However, I'm confused about how to keep these benefits when the method under test happens to be virtual.<br>
  In this case I <em>do</em> want to call the real code for that one method, while still having the rest of the class loosely mocked.</p>
  
  <p>All I have found in my searching is that I could set <code>mock.CallBase = true</code> to ensure that the method gets called. However, that affects the whole class. I don't want to do that because it puts me in a dilemma about all the other properties and methods in the class that hide call dependencies: if CallBase is true then I have to either</p>
  
  <ol>
  <li>Setup stubs for all of the properties and methods that hide dependencies -- Even though my test doesn't think it needs to care about those dependencies, or</li>
  <li>Hope that I don't forget to Setup any stubs (and that no new dependencies get added to the code in the future) -- Risk unit tests hitting a real dependency.</li>
  </ol>
  
  <p>What I think I want is something like:<br>
  <code>mock.Setup(m =&gt; m.VirtualMethod()).CallBase();</code><br>
  so that when I call <code>mock.Object.VirtualMethod()</code>, Moq calls into the real implementation...</p>
  
  <p><strong>Q: With Moq, is there any way to test a virtual method, when I mocked the class to stub just a few dependencies? I.e. Without resorting to <em>CallBase=true</em> and having to stub all of the dependencies?</strong></p>
  
  <hr>
  
  <p><em>Example code to illustrate</em><br>
     <em>(uses MSTest, InternalsVisibleTo DynamicProxyGenAssembly2)</em></p>
  
  <p>In the following example, <code>TestNonVirtualMethod</code> passes, but <code>TestVirtualMethod</code> fails - returns null.</p>
  
  <pre><code>public class Foo
  {
      public string NonVirtualMethod() { return GetDependencyA(); }
      public virtual string VirtualMethod() { return GetDependencyA();}
  
      internal virtual string GetDependencyA() { return "! Hit REAL Dependency A !"; }
      // [... Possibly many other dependencies ...]
      internal virtual string GetDependencyN() { return "! Hit REAL Dependency N !"; }
  }
  
  [TestClass]
  public class UnitTest1
  {
      [TestMethod]
      public void TestNonVirtualMethod()
      {
          var mockFoo = new Mock&lt;Foo&gt;();
          mockFoo.Setup(m =&gt; m.GetDependencyA()).Returns(expectedResultString);
  
          string result = mockFoo.Object.NonVirtualMethod();
  
          Assert.AreEqual(expectedResultString, result);
      }
  
      [TestMethod]
      public void TestVirtualMethod() // Fails
      {
          var mockFoo = new Mock&lt;Foo&gt;();
          mockFoo.Setup(m =&gt; m.GetDependencyA()).Returns(expectedResultString);
          // (I don't want to setup GetDependencyB ... GetDependencyN here)
  
          string result = mockFoo.Object.VirtualMethod();
  
          Assert.AreEqual(expectedResultString, result);
      }
  
      string expectedResultString = "Hit mock dependency A - OK";
  }
  </code></pre>