/// <summary>
        /// Configures LightBDD to use Autofac container described by <paramref name="builder"/>.
        /// </summary>
        /// <param name="configuration">Configuration.</param>
        /// <param name="builder">Autofac container builder.</param>
        public static DependencyContainerConfiguration UseAutofac(
            this DependencyContainerConfiguration configuration,
            ContainerBuilder builder)
        {
            var container = new AutofacContainer();

            container.AutofacScope = container.RegisterSelf(builder).Build();

            return(configuration.UseContainer(container));
        }
Пример #2
0
        /// <summary>
        /// Configures LightBDD to use provided <paramref name="container"/> Autofac container.<br/>
        /// Please note that <paramref name="container"/> will not be disposed by LightBDD after test run, but rather treat it as externally owned instance
        /// - use <see cref="UseAutofac(DependencyContainerConfiguration,ContainerBuilder)"/> if container should be fully managed by LightBDD.
        /// </summary>
        /// <param name="configuration">Configuration.</param>
        /// <param name="container">Container to use.</param>
        public static DependencyContainerConfiguration UseAutofac(
            this DependencyContainerConfiguration configuration,
            ILifetimeScope container)
        {
            var autofacScope = new AutofacContainer();

            autofacScope.AutofacScope = container.BeginLifetimeScope(builder => autofacScope.RegisterSelf(builder));

            configuration.UseContainer(autofacScope);
            return(configuration);
        }
        /// <summary>
        /// Configures LightBDD to use provided <paramref name="container"/> Autofac container, where <paramref name="takeOwnership"/> specifies if LightBDD should control container disposal or not.<br/>
        /// Please note that the new scope will be created to handle injections for LightBDD.<br/>
        /// </summary>
        /// <param name="configuration">Configuration.</param>
        /// <param name="container">Container to use.</param>
        /// <param name="takeOwnership">If true, the container will be disposed by LightBDD after tests are finished.</param>
        public static DependencyContainerConfiguration UseAutofac(
            this DependencyContainerConfiguration configuration,
            ILifetimeScope container,
            bool takeOwnership)
        {
            var autofacContainer = new AutofacContainer();

            autofacContainer.AutofacScope = container.BeginLifetimeScope(builder => autofacContainer.RegisterSelf(builder));
            if (takeOwnership)
            {
                autofacContainer.ParentScope = container;
            }

            return(configuration.UseContainer(autofacContainer));
        }
Пример #4
0
        private void AssertRegistration(Testable instance, CompositeStep step, bool shouldTakeOwnership)
        {
            var      container = new DependencyContainerConfiguration().DependencyContainer;
            Testable actual;

            using (var scope = container.BeginScope(step.SubStepsContext.ScopeConfigurator))
            {
                actual = (Testable)step.SubStepsContext.ContextResolver(scope);
                if (instance != null)
                {
                    Assert.That(actual, Is.SameAs(instance));
                }
            }
            Assert.That(actual.Disposed, Is.EqualTo(shouldTakeOwnership));
        }
Пример #5
0
        /// <summary>
        /// Configures LightBDD to use provided <paramref name="serviceProvider"/> provider, where <paramref name="takeOwnership"/> specifies if LightBDD should control provider disposal or not.<br/>
        /// Please note that the new scope will be created to handle injections for LightBDD.<br/>
        /// </summary>
        /// <param name="configuration">Configuration.</param>
        /// <param name="serviceProvider">Service provider to use.</param>
        /// <param name="takeOwnership">If true, the provider will be disposed by LightBDD after tests are finished.</param>
        public static DependencyContainerConfiguration UseContainer(
            this DependencyContainerConfiguration configuration, IServiceProvider serviceProvider, bool takeOwnership)
        {
            var container = new DiContainer(serviceProvider.CreateScope(), new ContainerOverrides());

            if (takeOwnership)
            {
                if (serviceProvider is IDisposable disposableParent)
                {
                    container.AddDisposable(disposableParent);
                }
                else
                {
                    throw new ArgumentException($"The provided {serviceProvider.GetType().FullName} is not {nameof(IDisposable)} and LightBDD cannot take proper ownership of the provider. Please consider specifying {nameof(takeOwnership)} parameter to be false and manual disposal of the provider.", nameof(serviceProvider));
                }
            }

            return(configuration.UseContainer(container));
        }
Пример #6
0
        /// <summary>
        /// Configures LightBDD to use DI container described by <paramref name="serviceProvider"/> provider, and allows customizing container behaviour with <paramref name="options"/> delegate.<br/>
        /// </summary>
        public static DependencyContainerConfiguration UseContainer(
            this DependencyContainerConfiguration configuration, IServiceProvider serviceProvider, Action <DiContainerOptions> options)
        {
            var containerOptions = new DiContainerOptions();

            options?.Invoke(containerOptions);

            var scope     = new NestingContainerScope(serviceProvider.CreateScope(), containerOptions.ShouldEnableScopeNestingWithinScenarios);
            var container = new DiContainer(scope, new ContainerOverrides());

            if (containerOptions.ShouldTakeOwnership)
            {
                if (serviceProvider is IDisposable disposableParent)
                {
                    container.AddDisposable(disposableParent);
                }
                else
                {
                    throw new ArgumentException($"The provided {serviceProvider.GetType().FullName} is not {nameof(IDisposable)} and LightBDD cannot take proper ownership of the provider. Please consider disabling takeOwnership configuration flag and manual disposal of the provider.", nameof(serviceProvider));
                }
            }

            return(configuration.UseContainer(container));
        }
        private static object ResolveInstance(CompositeStep stepGroup)
        {
            var container = new DependencyContainerConfiguration().DependencyContainer.BeginScope(stepGroup.SubStepsContext.ScopeConfigurator);

            return(stepGroup.SubStepsContext.ContextResolver(container));
        }
Пример #8
0
 /// <summary>
 /// Configures LightBDD to use DI container described by <paramref name="serviceProvider"/> provider, where <paramref name="takeOwnership"/> specifies if LightBDD should control provider disposal or not.<br/>
 /// Please note that the new scope will be created to handle injections for LightBDD.<br/>
 /// </summary>
 /// <param name="configuration">Configuration.</param>
 /// <param name="serviceProvider">Service provider to use.</param>
 /// <param name="takeOwnership">If true, the provider will be disposed by LightBDD after tests are finished.</param>
 public static DependencyContainerConfiguration UseContainer(
     this DependencyContainerConfiguration configuration, IServiceProvider serviceProvider, bool takeOwnership)
 {
     return(UseContainer(configuration, serviceProvider, opt => opt.TakeOwnership(takeOwnership)));
 }
Пример #9
0
 public static DependencyContainerConfiguration UseContainer(
     this DependencyContainerConfiguration configuration, IServiceProvider serviceProvider)
 {
     return(UseContainer(configuration, serviceProvider, false));
 }
 public static DependencyContainerConfiguration UseAutofac(
     this DependencyContainerConfiguration configuration,
     ILifetimeScope container)
 {
     return(configuration.UseAutofac(container, false));
 }
Пример #11
0
 /// <summary>
 /// Configures LightBDD to use DI container described by <paramref name="serviceProvider"/>.
 /// Please note that the new scope will be created to handle injections for LightBDD.
 /// </summary>
 /// <param name="configuration">Configuration.</param>
 /// <param name="serviceProvider">Service provider instance.</param>
 public static DependencyContainerConfiguration UseContainer(
     this DependencyContainerConfiguration configuration, IServiceProvider serviceProvider)
 {
     return(configuration.UseContainer(new DiContainer(serviceProvider.CreateScope(), new ContainerOverrides())));
 }