public void NativeContainerIsRegistered() { var container = new ContainerExtension(); object resolved = container.Resolve <INativeContainer>(); Assert.Same(container.Instance, resolved); }
public void RegisterManyRegistersServicesAsTransients() { var container = new ContainerExtension(); container.RegisterMany <AggregateService>(); Assert.NotSame(container.Resolve <IServiceA>(), container.Resolve <IServiceA>()); }
public void IContainerExtensionIsRegistered() { var container = new ContainerExtension(); object resolved = container.Resolve <IContainerExtension>(); Assert.Same(container, resolved); }
public void ThrowsContainerResolutionExceptionForUnregisteredNamedPage() { var container = new ContainerExtension(); var ex = Record.Exception(() => container.Resolve <object>("missing")); Assert.NotNull(ex); Assert.IsType <ContainerResolutionException>(ex); }
public void ThrowsContainerResolutionExceptionForUnregisteredService() { var container = new ContainerExtension(); var ex = Record.Exception(() => container.Resolve <IServiceMock>()); Assert.NotNull(ex); Assert.IsType <ContainerResolutionException>(ex); }
public void AutomaticTransientResolutionOfConcreteType() { var container = new ContainerExtension(); var resolved1 = container.Resolve <ServiceMock>(); var resolved2 = container.Resolve <ServiceMock>(); Assert.NotNull(resolved1); Assert.NotSame(resolved1, resolved2); }
public void LocatesImplementationType() { var container = new ContainerExtension(); container.Register <IServiceMock, ServiceMock>(); var type = container.GetRegistrationType(typeof(IServiceMock)); Assert.NotNull(type); Assert.Equal(typeof(ServiceMock), type); }
public void RegisterManySingletonRegistersAllInterfacesByDefault() { var container = new ContainerExtension(); container.RegisterManySingleton <AggregateService>(); Assert.IsType <AggregateService>(container.Resolve <IServiceA>()); Assert.IsType <AggregateService>(container.Resolve <IServiceB>()); Assert.IsType <AggregateService>(container.Resolve <IServiceC>()); }
public void RegisterNamedInstanceReturnsSameInstance() { var instance = new ServiceMock(); var container = new ContainerExtension(); container.RegisterInstance <object>(instance, "Test"); var resolved = container.Resolve <object>("Test"); Assert.Same(instance, resolved); }
public void LocatesImplementationTypeForNamedService() { var container = new ContainerExtension(); container.RegisterForNavigation <ViewAMock>(); var type = container.GetRegistrationType(nameof(ViewAMock)); Assert.NotNull(type); Assert.Equal(typeof(ViewAMock), type); }
public void RegisterNamedConcreteTypeCreatesTransient() { var container = new ContainerExtension(); container.Register <ServiceMock>("Test"); var resolved1 = container.Resolve <ServiceMock>("Test"); var resolved2 = container.Resolve <ServiceMock>("Test"); Assert.NotNull(resolved1); Assert.NotSame(resolved1, resolved2); }
public void RegisterSingletonConcreteTypeCreatesSingleton() { var container = new ContainerExtension(); container.RegisterSingleton <ServiceMock>(); var resolved1 = container.Resolve <ServiceMock>(); var resolved2 = container.Resolve <ServiceMock>(); Assert.NotNull(resolved1); Assert.Same(resolved1, resolved2); }
public void RegisterManySingletonOnlyRegistersSpecifiedInterfaces() { var container = new ContainerExtension(); container.RegisterManySingleton <AggregateService>(typeof(IServiceA), typeof(IServiceB)); Assert.IsType <AggregateService>(container.Resolve <IServiceA>()); Assert.IsType <AggregateService>(container.Resolve <IServiceB>()); var ex = Record.Exception(() => container.Resolve <IServiceC>()); Assert.NotNull(ex); }
public void RegisterManySingletonUsesSharedInstanceForAllServices() { var container = new ContainerExtension(); container.RegisterManySingleton <AggregateService>(); var serviceA = container.Resolve <IServiceA>(); var serviceB = container.Resolve <IServiceB>(); var serviceC = container.Resolve <IServiceC>(); Assert.Same(serviceA, serviceB); Assert.Same(serviceB, serviceC); }
public void RegisterServiceMappingCreatesTransient() { var container = new ContainerExtension(); container.Register <IServiceMock, ServiceMock>(); var resolved1 = container.Resolve <IServiceMock>(); var resolved2 = container.Resolve <IServiceMock>(); Assert.NotNull(resolved1); Assert.NotNull(resolved2); Assert.IsType <ServiceMock>(resolved1); Assert.IsType <ServiceMock>(resolved2); Assert.NotSame(resolved1, resolved2); }
public void RegisterSingletonWithFactoryAndContainerProvider() { var container = new ContainerExtension(); container.Register <IServiceMock, ServiceMock>(nameof(ServiceMock)); container.RegisterSingleton <IServiceMock>(CreateServiceWithContainerProvider); var resolved = container.Resolve <IServiceMock>(); Assert.NotNull(resolved); Assert.IsType <ServiceMock>(resolved); var serviceMock = resolved as ServiceMock; var resolved2 = container.Resolve <IServiceMock>(); Assert.Same(resolved, resolved2); }
public void LocatesUnregisteredServiceWithMissingRegistration() { ContainerLocator.ResetContainer(); var container = new ContainerExtension(); ContainerLocator.SetContainerExtension(() => container); var ex = Record.Exception(() => container.Resolve <ConstructorArgumentViewModel>()); Assert.IsType <ContainerResolutionException>(ex); var cre = ex as ContainerResolutionException; var errors = cre.GetErrors(); Assert.Contains(errors, x => x.Value is ContainerResolutionException innerCre && innerCre.ServiceType == typeof(IServiceMock) && innerCre.Message == ContainerResolutionException.MissingRegistration); }
public void LocatesUnregisteredServiceType() { ContainerLocator.ResetContainer(); var container = new ContainerExtension(); ContainerLocator.SetContainerExtension(() => container); var ex = Record.Exception(() => container.Resolve <ConstructorArgumentViewModel>()); Assert.IsType <ContainerResolutionException>(ex); var cre = ex as ContainerResolutionException; var errors = cre.GetErrors(); Assert.Contains(typeof(IServiceMock), errors.Types); }
public void RegisterSingletonSupportsFuncInjection() { var container = new ContainerExtension(); container.RegisterSingleton <IServiceMock, ServiceMock>(); var func = container.Resolve <Func <IServiceMock> >(); Assert.NotNull(func); var instance = func(); Assert.IsType <ServiceMock>(instance); var func2 = container.Resolve <Func <IServiceMock> >(); Assert.Same(instance, func2()); }
public void RegisterSingletonWithFactory() { var container = new ContainerExtension(); container.RegisterSingleton <IServiceMock>(CreateService); var resolved = container.Resolve <IServiceMock>(); Assert.NotNull(resolved); Assert.IsType <ServiceMock>(resolved); var serviceMock = resolved as ServiceMock; Assert.Equal("Created through a factory", serviceMock.SomeProperty); var resolved2 = container.Resolve <IServiceMock>(); Assert.Same(resolved, resolved2); }
public void RegisterSingletonNamedServiceMappingCreatesSingleton() { var container = new ContainerExtension(); container.RegisterSingleton <IServiceMock, ServiceMock>("Test"); var resolved1 = container.Resolve <IServiceMock>("Test"); var resolved2 = container.Resolve <IServiceMock>("Test"); var ex = Record.Exception(() => container.Resolve <IServiceMock>()); Assert.NotNull(ex); Assert.NotNull(resolved1); Assert.NotNull(resolved2); Assert.IsType <ServiceMock>(resolved1); Assert.IsType <ServiceMock>(resolved2); Assert.Same(resolved1, resolved2); }
public void RegisterScopedConcreteTypeCreatesScoped() { var container = new ContainerExtension(); container.RegisterScoped <ServiceMock>(); var resolved1 = container.Resolve <ServiceMock>(); var resolved2 = container.Resolve <ServiceMock>(); Assert.NotNull(resolved1); Assert.Same(resolved1, resolved2); container.CreateScope(); var resolved3 = container.Resolve <ServiceMock>(); Assert.NotSame(resolved1, resolved3); }
public void GetErrorsLocatesTargetInvocationException() { ContainerLocator.ResetContainer(); var container = new ContainerExtension(); ContainerLocator.SetContainerExtension(() => container); container.Register <object, BadView>("BadView"); var ex = Record.Exception(() => container.Resolve <object>("BadView")); Assert.IsType <ContainerResolutionException>(ex); var cre = ex as ContainerResolutionException; var errors = cre.GetErrors(); Assert.Contains(errors, x => x.Value is TargetInvocationException); }
public void GetErrorsLocatesIssueWithBadView() { ContainerLocator.ResetContainer(); var container = new ContainerExtension(); ContainerLocator.SetContainerExtension(() => container); container.Register <object, BadView>("BadView"); var ex = Record.Exception(() => container.Resolve <object>("BadView")); Assert.IsType <ContainerResolutionException>(ex); var cre = ex as ContainerResolutionException; var errors = cre.GetErrors(); Assert.Contains(typeof(BadView), errors.Types); }
public void RegisterSingletonSupportsLazyInjection() { var container = new ContainerExtension(); container.RegisterSingleton <IServiceMock, ServiceMock>(); var lazy = container.Resolve <Lazy <IServiceMock> >(); Assert.NotNull(lazy); Assert.False(lazy.IsValueCreated); var instance = lazy.Value; Assert.IsType <ServiceMock>(instance); var lazy2 = container.Resolve <Lazy <IServiceMock> >(); Assert.Same(instance, lazy2.Value); }
public void GetErrorsDoesNotThrowException() { ContainerLocator.ResetContainer(); var container = new ContainerExtension(); ContainerLocator.SetContainerExtension(() => container); container.Register <object, BadView>("BadView"); var ex = Record.Exception(() => container.Resolve <object>("BadView")); Assert.IsType <ContainerResolutionException>(ex); var cre = ex as ContainerResolutionException; ContainerResolutionErrorCollection errors = null; var ex2 = Record.Exception(() => errors = cre.GetErrors()); Assert.Null(ex2); }
public void RegisterScopedWithFactoryAndContainerProvider() { var container = new ContainerExtension(); container.Register <IServiceMock, ServiceMock>(nameof(ServiceMock)); container.RegisterScoped <IServiceMock>(CreateServiceWithContainerProvider); var resolved1 = container.Resolve <IServiceMock>(); Assert.NotNull(resolved1); Assert.IsType <ServiceMock>(resolved1); var resolved2 = container.Resolve <IServiceMock>(); Assert.Same(resolved1, resolved2); container.CreateScope(); var resolved3 = container.Resolve <IServiceMock>(); Assert.NotSame(resolved2, resolved3); }
public void RegisterScopedWithFactory() { var container = new ContainerExtension(); container.RegisterScoped <IServiceMock>(CreateService); var resolved1 = container.Resolve <IServiceMock>(); Assert.NotNull(resolved1); Assert.IsType <ServiceMock>(resolved1); var serviceMock = resolved1 as ServiceMock; Assert.Equal("Created through a factory", serviceMock.SomeProperty); var resolved2 = container.Resolve <IServiceMock>(); Assert.Same(resolved1, resolved2); container.CreateScope(); var resolved3 = container.Resolve <IServiceMock>(); Assert.NotSame(resolved2, resolved3); }