title: Mock Autofac.IComponentContext with unit tests c#
body: <p>I am trying to mock <code>IComponentContext</code> with NSubstitute as below:</p>
  
  <pre><code>[TestClass()]
  public class SyncRepositoryFactoryTests
  {
      private IComponentContext _container;
      private SyncRepositoryFactory _factory;
  
      [TestInitialize]
      public void Initialize()
      {
          _container = Substitute.For&lt;IComponentContext&gt;();
          _factory = new SyncRepositoryFactory(_container);
      }
  
      [TestMethod]
      public void Get_SyncRepositoryOfITestEntity_Success()
      {
          var repository = Substitute.For&lt;IRepository&lt;TestEntity&gt;&gt;();
          _container.Resolve&lt;IRepository&lt;TestEntity&gt;&gt;().Returns(repository);
  
          var result = _factory.Get&lt;ITestEntity&gt;();
  
          Assert.IsNotNull(result);
          Assert.IsTrue(result is ISyncRepository&lt;ITestEntity&gt;);
      }
  
      public interface ITestEntity 
      {
      }
  
      public class TestEntity : ITestEntity
      {
      }
  }
  </code></pre>
  
  <p>but I get an exception:</p>
  
  <pre>    ComponentNotRegisteredException: The requested service 
          'Hvb.eMarketAdvisor.Repository.SharePoint.IRepository`1[[Hvb.eMarketAdvisor.
          Repository.SharePoint.Tests.Units.SyncRepositoryFactoryTests+TestEntity, 
          Hvb.eMarketAdvisor.Repository.SharePoint.Tests.Units, 
      Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered. 
  
      To avoid this exception, either register a component to provide the service,
      check for service registration using IsRegistered(), or use the ResolveOptional()
      method to resolve an optional dependency.
  </pre>
  
  <p>As I try to create a mock why does <code>IComponentContext</code> try to resolve dependency?</p>