title: partial mocking with Moq and Castle Windsor
body: <blockquote>
    <p>versions    Castle Windsor 3.3.0.0      Moq 4.2.1507.118</p>
  </blockquote>
  
  <p>it appears that Moq comes with <em>CallBase</em> setting that, when enabled, delegates calls to any non-setup methods down to base object. </p>
  
  <pre><code>var mock = new Mock&lt;MyService&gt; {CallBase = true};
  </code></pre>
  
  <p>unfortunately as soon as I register it with Castle Windsor, all non mocked methods are returning a null immediately, underlying logic is no longer called. </p>
  
  <pre><code>         Container.Register(Component
              .For&lt;MyService&gt;()
              .Instance(mock.Object)
              .LifeStyle.Singleton
              );
  </code></pre>
  
  <p>I tried a few different lifestyles as well. </p>
  
  <p>so when I do this: </p>
  
  <pre><code>mock.setup(x =&gt; x.SomeMethod).Returns(myMockedData);
  </code></pre>
  
  <p>this works as expected:</p>
  
  <pre><code>Container.Resolve&lt;MyService&gt;().SomeMethod();
  </code></pre>
  
  <p>but now this returns a null:</p>
  
  <pre><code>Container.Resolve&lt;MyService&gt;().AnotherMethod(); 
  </code></pre>
  
  <p>as soon as I remove the mock registration, AnotherMethod starts working as before. </p>
  
  <p><strong>more specific code example</strong></p>
  
  <pre><code>public class Auth : IAuth
  {
     // mocked succesfully
     public IEnumerable&lt;T&gt; AuthorizeData&lt;T&gt;(IEnumerable&lt;T&gt; data)  { ... }
  
      // never gets called
      public virtual List&lt;User&gt; GetAllUsers() {
          return new List&lt;User&gt;{new User{FIRST_NAME = "test"}};
      }
  }
  </code></pre>
  
  <p>mock method</p>
  
  <pre><code>    protected static Mock&lt;T&gt; GetMock&lt;T&gt;() where T : class {
  
          var mock = new Mock&lt;T&gt; {CallBase = true, DefaultValue = DefaultValue.Mock};
          IoC.Container.Register(Component
              .For&lt;T&gt;()
              .Instance(mock.Object)
              .LifeStyle.Transient
              );
          return mock;
      }
  </code></pre>
  
  <p>mock setup</p>
  
  <pre><code>GetMock&lt;IAuth&gt;().Setup(x =&gt; x.AuthorizeData(It.IsAny&lt;IEnumerable&lt;DO&gt;&gt;()))
                  .Returns((IEnumerable&lt;DO&gt; data) =&gt; data); // pass through input data (do not authorize
  </code></pre>
  
  <p>to sum up again, AuthorizeData is mocked successfully, GetAllUsers() returns an empty list. and original class never gets called </p>