public void When_a_type_cannot_be_resolved_via_GetService_it_returns_null()
        {
            var container = new PocketContainer() as IServiceProvider;

            var resolved = container.GetService(typeof(string));

            resolved.Should().BeNull();
        }
        public void Resolve_implements_IServiceProvider_GetService()
        {
            var container = new PocketContainer()
                            .Register(_ => "hello")
                            as IServiceProvider;

            var resolved = container.GetService(typeof(string));

            resolved.Should().Be("hello");
        }
        public void AddSingleton_behaves_as_expected()
        {
            var services = new ServiceCollection()
                           .AddSingleton <IEnumerable <string> >(new List <string>());
            var container = new PocketContainer()
                            .AsServiceProvider(services);

            var fromContainer = container.GetService <IEnumerable <string> >();
            var fromServices  = services.BuildServiceProvider()
                                .GetService <IEnumerable <string> >();

            fromContainer.Should().BeSameAs(fromServices);
        }
        public void AddFactory_behaves_as_expected()
        {
            var services = new ServiceCollection()
                           .AddTransient <IAmAnInterface>(s => new HasDefaultCtor());
            var container = new PocketContainer()
                            .AsServiceProvider(services);

            var fromContainer = container.GetService <IAmAnInterface>();
            var fromServices  = services.BuildServiceProvider()
                                .GetService <IAmAnInterface>();

            fromContainer.Should().BeOfType <HasDefaultCtor>();
            fromContainer.Should().NotBeSameAs(fromServices);
        }
        public void When_multiple_transients_have_been_registered_an_IEnumerable_can_be_resolved_which_contains_them_all()
        {
            var services = new ServiceCollection()
                           .AddTransient <IAmAnInterface, HasDefaultCtor>()
                           .AddTransient <IAmAnInterface, AlsoHasDefaultCtor>();

            var container = new PocketContainer()
                            .AccumulateRegistrations()
                            .AsServiceProvider(services);

            var fromServices = services.BuildServiceProvider()
                               .GetService <IEnumerable <IAmAnInterface> >();

            var fromContainer = container.GetService <IEnumerable <IAmAnInterface> >();

            fromContainer.Count().Should().Be(2);
            fromContainer.Should().Contain(o => o is HasDefaultCtor);
            fromContainer.Should().Contain(o => o is AlsoHasDefaultCtor);
        }
        public void Transient_instances_resolved_from_the_root_are_disposed_when_the_container_is_disposed()
        {
            var wasDisposed = false;

            var services = new ServiceCollection()
                           .AddTransient(_ => Disposable.Create(() =>
            {
                wasDisposed = true;
            }));

            var serviceProvider = new PocketContainer()
                                  .AsServiceProvider(services);

            serviceProvider.GetService <IDisposable>();

            ((IDisposable)serviceProvider).Dispose();

            wasDisposed.Should().BeTrue();
        }
        public void Transient_instances_resolved_from_outside_a_scope_are_disposed_when_the_scope_is_disposed()
        {
            var wasDisposed = false;

            var services = new ServiceCollection()
                           .AddTransient(_ => Disposable.Create(() =>
            {
                wasDisposed = true;
            }));

            var serviceProvider = new PocketContainer()
                                  .AsServiceProvider(services);

            var scope = serviceProvider.CreateScope();

            serviceProvider.GetService <IDisposable>();

            scope.Dispose();

            wasDisposed.Should().BeFalse();
        }