MinistryOfMagic / xunit.frameworks.autofac

Use Autofac to resolve xUnit test classes
MIT License
6 stars 3 forks source link

Failed to inject concrete class #13

Open jvmlet opened 2 years ago

jvmlet commented 2 years ago

Hello I'm trying to inject WireMockServer instance via module:

 public class WireMockServerModule : Module {
        protected override void Load(ContainerBuilder builder) {
            builder.Register(c =>WireMockServer.Start()) // WireMockServer.Start() returns  WireMockServer type, which implements  IWireMockServer interface
                .As<IWireMockServer>()
                .AsSelf()
                .SingleInstance();
        }
    }
public class MyClientTestFixture : INeedModule<WireMockServerModule> {
  }

 [UseAutofacTestFramework]
 public class MyClientTest : IClassFixture<MyClientTestFixture> {
        private readonly WireMockServer _server;

        public MyClientTest ( WireMockServer server) {
            _server =   server;
        }
}

This fails with

Autofac.Core.Activators.Reflection.NoConstructorsFoundException
No accessible constructors were found for the type 'WireMock.Server.WireMockServer'.
   at Autofac.Core.Activators.Reflection.DefaultConstructorFinder.GetDefaultPublicConstructors(Type type)
   at Autofac.Core.Activators.Reflection.DefaultConstructorFinder.FindConstructors(Type targetType)
   at Autofac.Core.Activators.Reflection.ReflectionActivator.ConfigurePipeline(IComponentRegistryServices componentRegistryServices, IResolvePipelineBuilder pipelineBuilder)
   at Autofac.Core.Registration.ComponentRegistration.BuildResolvePipeline(IComponentRegistryServices registryServices, IResolvePipelineBuilder pipelineBuilder)
   at Autofac.Core.Registration.ComponentRegistration.BuildResolvePipeline(IComponentRegistryServices registryServices)
   at Autofac.Core.Registration.DefaultRegisteredServicesTracker.AddRegistration(IComponentRegistration registration, Boolean preserveDefaults, Boolean originatedFromDynamicSource)

(WireMockServerModule::Load is invoked, but not the callback itself)

However, when I depend on IWireMockServer, it works as expected

  public MyClientTest ( IWireMockServer server) {
            _server =  (WireMockServer) server;
        }

and both WireMockServerModule::Load and callback are invoked. Please advise.