title: Moq - Calling two functions in the same class, one real, one mocked
body: <p>I have a piece of code that I may not be able to change.  Ideally I'd move the Publish method into another class.  Mocking would become a trivial operation and I'd be good.  Here's a simplified example.</p>
  
  <pre><code>public class Entity
  {
    private IController _controller;
  
    // Omitted: Constructor where IController is assigned
  
    public virtual int Process()
    {
      return Publish(_controller.DoSomeStuff());
    }
  
    public virtual int Publish(int val)
    {
      Console.WriteLine(val);  // This is actually a call to an external system.
      return val;
    }
  }
  </code></pre>
  
  <p>What I want to do is call the real <strong>Process</strong> method and a mocked <strong>Publish</strong> method.  The value of <strong>_controller</strong> will be mocked as well.</p>
  
  <p>I tried using moq and doing:</p>
  
  <pre><code>var mock = new Mock&lt;IController&gt;();
  mock.Setup(m =&gt; m.DoSomeStuff()).Returns(11);
  
  var mock2 = new Mock&lt;Entity_Accessor&gt;();
  mock2.CallBase = true;
  mock2.Setup(a =&gt; a.Publish(It.IsAny&lt;int&gt;())).Returns(999);
  mock2.Object._controller = mock.Object;
  
  int result = mock2.Object.Process();
  Assert.AreEqual(999, result);
  </code></pre>
  
  <p>When I run this test, the value of 11 is returned instead of 999.  If I remove mock2.CallBase = true, the entire class is mocked which returns default values for every function.</p>
  
  <p>Is it possible to mock just the Publish method in this case while retaining the original implementation of Process?</p>
  
  <p>Is it possible to achieve the above with a different mocking framework that isn't Typemock or Justmock (not sure if the customer will foot the bill for multiple copies of a mocking tool)?</p>
  
  <p>Thank you!</p>