title: NSubstitute and Unity
body: <p>I am currently trying to learn DI &amp; Mocking, with Unity and NSubstitute. I am also using an automocking extension taken from this question: <a href="https://stackoverflow.com/questions/10627406/is-this-possible-with-unity-instead-of-castle-windsor">Is this possible with Unity (Instead of Castle Windsor)?</a></p>
  
  <p>In my unit test below I am trying to set an NSubstitute return value of 10 from my method Add(). However when debugging through the controller call the assigned value is the default 0 rather than the expected 10. The proxy does not seem to be intercepting the method call.</p>
  
  <p>I suspect this is caused by not registering my Types/container correctly, anybody able to point me in the right direction?</p>
  
  <pre><code>[TestFixture]
  public class ApplicationControllerTests
  {
      private IUnityContainer _container;
      private ApplicationController _controller;
      private ISampleService _sampleService;
  
      [SetUp]
      public void SetUp()
      {
          _container = new UnityContainer().AddExtension(new AutoMockingContainerExtension());
          _controller = _container.Resolve&lt;ApplicationController&gt;();
          _sampleService = _container.Resolve&lt;ISampleService&gt;();
      }
  
      [Test]
      public void TestSampleService()
      {
          // This line is not working
          _sampleService.Add(Arg.Any&lt;int&gt;(), Arg.Any&lt;int&gt;()).Returns(10);
  
          var result = _controller.Index();
  
          _sampleService.Received().Add(Arg.Any&lt;int&gt;(), Arg.Any&lt;int&gt;());
      }
  }
  
  public class AutoMockingContainerExtension : UnityContainerExtension
  {
      protected override void Initialize()
      {
          var strategy = new AutoMockingBuilderStrategy(Container);
  
          Context.Strategies.Add(strategy, UnityBuildStage.PreCreation);
      }
  
      class AutoMockingBuilderStrategy : BuilderStrategy
      {
          private readonly IUnityContainer _container;
  
          public AutoMockingBuilderStrategy(IUnityContainer container)
          {
              _container = container;
          }
  
          public override void PreBuildUp(IBuilderContext context)
          {
              var key = context.OriginalBuildKey;
  
              if (key.Type.IsInterface &amp;&amp; !_container.IsRegistered(key.Type))
                  context.Existing = CreateSubstitute(key.Type);
          }
  
          private static object CreateSubstitute(Type type)
          {
              return Substitute.For(new[] { type }, null);
          }
      }
  }
  </code></pre>
  
  <p>And my controller code</p>
  
  <pre><code>public class ApplicationController : BaseController
  {
      private readonly ISampleService _sampleService;
  
      public ApplicationController(ISampleService sampleService)
      {
          _sampleService = sampleService;
      }
  
      public ActionResult Index()
      {
          var result = _sampleService.Add(2, 3);
  
          // result is 0, expected 10 ??
  
          return View();
      }
  }
  
  public interface ISampleService
  {
      int Add(int first, int second);
  }
  
  public class SampleService : ISampleService
  {
      public int Add(int first, int second)
      {
          return first + second;
      }
  }
  </code></pre>