title: XUnit Test Constructor dependence injection with Autofac
body: <p>I am implementing Xunit with Autofac, I could make it work by below code:</p>
  <pre><code>using (var scoped = DbFixture.Container.Resolve&lt;UserReponsitory&gt;())
  {
      var result = (scoped.GetAll()).ToList().Count();
      Assert.Equal(2, result);
  }
  </code></pre>
  <p>But I want to inject UserReponsitory to test method instead of using <code>DbFixture.Container.Resolve</code>. Is it possible to make below code work?<br />
  UnitTest1.cs</p>
  <pre><code>namespace XUnitTestPro
  {
      public class UnitTest1:IClassFixture&lt;DbFixture&gt;
      {
          private IUserReponsitory _userReponsitory;
          public UnitTest1(IUserReponsitory userReponsitory)
          {
              _userReponsitory = userReponsitory;
          }
          [Fact]
          public void Test1()
          {
              //using (var scoped = DbFixture.Container.Resolve&lt;UserReponsitory&gt;())
              //{
              //    var result = (scoped.GetAll()).ToList().Count();
              //    Assert.Equal(2, result);
              //}
              var result = _userReponsitory.GetAll().ToList().Count();
              Assert.Equal(2, result);
          }
      }
  }
  </code></pre>
  <p>DbFixture.cs</p>
  <pre><code>namespace XUnitTestPro
  {
      public class DbFixture
      {
          public static IContainer Container { get; set; }
  
          public DbFixture()
          {
              var builder = new ContainerBuilder();
              var option = new DbContextOptionsBuilder&lt;UserContext&gt;().UseSqlServer(&quot;Server=(localdb)\\MSSQLLocalDB;Database=EFProject;Trusted_Connection=True;MultipleActiveResultSets=true&quot;).Options;
              UserContext context = new UserContext(option);
              builder.RegisterInstance(context).As&lt;UserContext&gt;();
  
              builder.RegisterType&lt;UserReponsitory&gt;().AsSelf().As&lt;IUserReponsitory&gt;();
  
              builder.RegisterAssemblyTypes(typeof(DbFixture).GetTypeInfo().Assembly);
  
              Container = builder.Build();
  
          }
      }
  }
  </code></pre>
  <p>At present, I got below error, it seems to be related with <code>IClassFixture&lt;DbFixture&gt;</code> and <code>public UnitTest1(IUserReponsitory userReponsitory)</code> are different.</p>
  <blockquote>
  <p>Message: The following constructor parameters did not have matching
  fixture data: IUserReponsitory userReponsitory</p>
  </blockquote>
  <p>Is there any way to achieve below code without call <code>DbFixture.Container.Resolve</code> which is similar to inject MVC Controller?</p>
  <pre><code>public UnitTest1(IUserReponsitory userReponsitory)
  {
      _userReponsitory = userReponsitory;
  }
  </code></pre>
  <p>In other words, how could I dependence inject Unit Test class?<br />
  Any help would be appreciated.</p>