title: In unit test when using nSubstitute and Autofixture as a DI container, how to get mocked object?
body: <p>I used to use Moq and AutoMoqer in unit tests, but my team has decided to change to NSubstitute.  We use DI heavily, so I'd like to be able to ask for a target to test and have that target automatically given all mocked objects to its constructor, or in other words a DI container that passes in mocks.  I also want to modify those mocked objects as necessary.</p>
  
  <p>Example using Moq/AutoMoq/MSTest</p>
  
  <pre><code>[TestMethod]
  public void ReturnSomeMethod_WithDependenciesInjectedAndD1Configured_ReturnsConfiguredValue()
  {
      const int expected = 3;
      var diContainer = new AutoMoq.AutoMoqer();
  
      var mockedObj = diContainer.GetMock&lt;IDependency1&gt;();
      mockedObj
          .Setup(mock =&gt; mock.SomeMethod())
          .Returns(expected);
  
      var target = diContainer.Resolve&lt;MyClass&gt;();
      int actual = target.ReturnSomeMethod();
  
      Assert.AreEqual(actual, expected);
  }
  
  public interface IDependency1
  {
      int SomeMethod();
  }
  
  public interface IDependency2
  {
      int NotUsedInOurExample();
  }
  
  public class MyClass
  {
      private readonly IDependency1 _d1;
      private readonly IDependency2 _d2;
  
      //please imagine this has a bunch of dependencies, not just two
      public MyClass(IDependency1 d1, IDependency2 d2)
      {
          _d1 = d1;
          _d2 = d2;
      }
  
      public int ReturnSomeMethod()
      {
          return _d1.SomeMethod();
      }
  }
  </code></pre>
  
  <p>Since my question was poorly worded and I have researched a bit more, I have found a way to get this working using NSubstitute/AutofacContrib.NSubstitute/XUnit:</p>
  
  <pre><code>[Fact]
  public void ReturnSomeMethod_WithDependenciesInjectedAndD1Configured_ReturnsConfiguredValue()
  {
      const int expected = 3;
      var autoSubstitute = new AutoSubstitute();
      autoSubstitute.Resolve&lt;IDependency1&gt;().SomeMethod().Returns(expected);
  
      var target = autoSubstitute.Resolve&lt;MyClass&gt;();
      int actual = target.ReturnSomeMethod();
  
      Assert.Equal(actual, expected);
  }
  
  public interface IDependency1
  {
      int SomeMethod();
  }
  
  public interface IDependency2
  {
      int NotUsedInOurExample();
  }
  
  public class MyClass
  {
      private readonly IDependency1 _d1;
      private readonly IDependency2 _d2;
  
      //please imagine this has a bunch of dependencies, not just two
      public MyClass(IDependency1 d1, IDependency2 d2)
      {
          _d1 = d1;
          _d2 = d2;
      }
  
      public int ReturnSomeMethod()
      {
          return _d1.SomeMethod();
      }
  }
  </code></pre>
  
  <p>I still have my original question.  How can I do this using AutoFixture.AutoNSubstitute as a DI container? </p>