public void AddExpressionBuilding_AfterContainerHasBeenLocked_ThrowsAnException() { // Arrange var container = ContainerFactory.New(); container.RegisterSingleton <IUserRepository>(new SqlUserRepository()); // The first use of the container locks the container. container.GetInstance <IUserRepository>(); // Act Action action = () => container.ExpressionBuilding += (s, e) => { }; // Assert AssertThat.Throws <InvalidOperationException>(action, "Registration of an event after the container is locked is illegal."); }
public void Verify_RootTypeCollectionWithDecoratorThatCanNotBeCreatedAtRuntime_ThrowsInvalidOperationException() { // Arrange var container = ContainerFactory.New(); // Root type container.Collection.Register <IPlugin>(new[] { typeof(PluginImpl) }); // FailingConstructorDecorator constructor throws an exception. container.RegisterDecorator(typeof(IPlugin), typeof(FailingConstructorPluginDecorator)); // Act Action action = () => container.Verify(); // Assert AssertThat.Throws <InvalidOperationException>(action); }
public void Verify_DecoratorWithDecorateeFactoryWithFailingDecorateeOfNonRootType_ThrowsExpectedException() { // Arrange var container = ContainerFactory.New(); container.Register <PluginConsumer>(); container.Register <IPlugin, FailingConstructorPlugin <Exception> >(); container.RegisterDecorator(typeof(IPlugin), typeof(PluginProxy), Lifestyle.Singleton); // Act Action action = () => container.Verify(); // Assert AssertThat.Throws <InvalidOperationException>(action); }
public void RemoveResolveUnregisteredType_AfterContainerHasBeenLocked_ThrowsAnException() { // Arrange var container = ContainerFactory.New(); container.RegisterInstance <IUserRepository>(new SqlUserRepository()); // The first use of the container locks the container. container.GetInstance <IUserRepository>(); // Act Action action = () => container.ResolveUnregisteredType -= (s, e) => { }; // Assert AssertThat.Throws <InvalidOperationException>(action, "Removal of an event after the container is locked is illegal."); }
public void AppendTo_CalledAfterTheFirstItemIsRequested_ThrowsExpectedException() { // Arrange var container = ContainerFactory.New(); var registration1 = Lifestyle.Transient.CreateRegistration <PluginImpl>(container); var registration2 = Lifestyle.Transient.CreateRegistration <PluginImpl2>(container); container.Collection.Append(typeof(IPlugin), registration1); var instances = container.GetAllInstances <IPlugin>().ToArray(); // Act Action action = () => container.Collection.Append(typeof(IPlugin), registration2); // Assert AssertThat.Throws <InvalidOperationException>(action); }
public void VisualizeObjectGraph_WhenCalledBeforeInstanceIsCreated_ThrowsAnInvalidOperationException() { // Arrange var container = new Container(); container.Register <RealTimeProvider>(); InstanceProducer producer = container.GetRegistration(typeof(RealTimeProvider)); // Act Action action = () => producer.VisualizeObjectGraph(); // Assert AssertThat.Throws <InvalidOperationException>(action, "When the instance hasn't been created or the Expression hasn't been built, there's not yet " + "enough information to visualize the object graph. Instead of returning an incorrect result " + "we expect the library to throw an exception here."); }
public void RegisterInstance_AfterCallingGetAllInstances_ThrowsException() { // Arrange var container = ContainerFactory.New(); container.Collection.Register <IUserRepository>(Type.EmptyTypes); var repositories = container.GetAllInstances <IUserRepository>(); // Calling count will iterate the collections. // The container will only get locked when the first item is retrieved. var count = repositories.Count(); // Act Action action = () => container.RegisterInstance <UserServiceBase>(new RealUserService(null)); // Assert AssertThat.Throws <InvalidOperationException>(action, "The container should get locked after a call to GetAllInstances."); }
public void Verify_FailingCollection_ThrowsException() { // Arrange var container = ContainerFactory.New(); IEnumerable <IUserRepository> repositories = from nullRepository in Enumerable.Repeat <IUserRepository>(null, 1) where nullRepository.ToString() == "This line fails with an NullReferenceException" select nullRepository; container.Collection.Register <IUserRepository>(repositories); // Act Action action = () => container.Verify(); // Assert AssertThat.Throws <InvalidOperationException>(action); }
public void RegisterByFunc_AfterCallingGetAllInstances_ThrowsException() { // Arrange var container = ContainerFactory.New(); container.Collection.Register <IUserRepository>(Type.EmptyTypes); var repositories = container.GetAllInstances <IUserRepository>(); // Only during iterating the collection, will the underlying container be called. This is a // Common Service Locator thing. var count = repositories.Count(); // Act Action action = () => container.Register <UserServiceBase>(() => new RealUserService(null)); // Assert AssertThat.Throws <InvalidOperationException>(action, "The container should get locked after a call to GetAllInstances."); }
public void Verify_DecoratorWithFuncDecorateeWithFailingConstructor_ThrowsTheExpectedException() { // Arrange var container = new Container(); container.Register <IPlugin, FailingPlugin>(); container.RegisterDecorator(typeof(IPlugin), typeof(PluginProxy), Lifestyle.Singleton); // This call will succeed, because it resolves: "new PluginProxy(() => new FailingPlugin())" and // that will not call the FailingPlugin constructor. container.GetInstance <IPlugin>(); // Act // This should still throw an exception. Action action = () => container.Verify(); // Assert AssertThat.Throws <InvalidOperationException>(action); }
public void AllowOverridingRegistrations_SetToFalse_ContainerDoesNotAllowOverridingRegistrationOfCollections() { // Arrange var container = new Container(new ContainerOptions { AllowOverridingRegistrations = false }); container.RegisterAll(typeof(IEventHandler <ClassEvent>), new[] { typeof(NonGenericEventHandler) }); // Act Action action = () => container.RegisterAll(typeof(IEventHandler <ClassEvent>), new[] { typeof(ClassConstraintEventHandler <ClassEvent>) }); // Assert AssertThat.Throws <InvalidOperationException>(action); }
public void Verify_RegistrationWithDecoratorThatCanNotBeCreatedAtRuntimeAndBuildExpressionCalledExplicitly_ThrowsInvalidOperationException() { // Arrange var container = ContainerFactory.New(); container.Register <IPlugin, PluginImpl>(); container.RegisterDecorator(typeof(IPlugin), typeof(FailingConstructorPluginDecorator)); container.GetRegistration(typeof(IPlugin)).BuildExpression(); // Act Action action = () => container.Verify(); // Assert // This test verifies a bug: Calling InstanceProducer.BuildExpression flagged the producer to be // skipped when calling Verify() while it was still possible that creating the instance would fail. AssertThat.Throws <InvalidOperationException>(action, "The call to BuildExpression should not trigger the verification of IPlugin to be skipped."); }
public void GetInstance_CalledAfterContainerLockingIsRaisedWhileThrowingAnException_StillLocksTheContainer() { // Arrange var container = ContainerFactory.New(); container.Register <ILogger, NullLogger>(); container.Options.ContainerLocking += (s, e) => { throw new Exception(); }; // Act // GetInstance should lock the container, even though ContainerLocking throws an exception AssertThat.Throws <Exception>(() => container.GetInstance <ILogger>()); // Assert Assert.IsTrue(container.IsLocked, "Container is expected to get locked; even when ContainerLocking throws an exception."); }
public void GetAllInstances_OnUnregisteredType_TriggersUnregisteredTypeResolution() { // Arrange bool resolveUnregisteredTypeWasTriggered = false; var container = ContainerFactory.New(); container.ResolveUnregisteredType += (s, e) => { if (e.UnregisteredServiceType == typeof(IEnumerable <Exception>)) { resolveUnregisteredTypeWasTriggered = true; } }; // Act Action action = () => container.GetAllInstances <Exception>(); // Assert AssertThat.Throws <ActivationException>(action); Assert.IsTrue(resolveUnregisteredTypeWasTriggered); }
public void GetInstance_CalledAfterContainerLockingIsRaisedWhileThrowingAnException_StillDisallowsMakingNewRegistrations() { // Arrange var container = ContainerFactory.New(); container.Register <ILogger, NullLogger>(); container.Options.ContainerLocking += (s, e) => { throw new Exception(); }; // GetInstance should lock the container, even though ContainerLocking throws an exception AssertThat.Throws <Exception>(() => container.GetInstance <ILogger>(), "Setup"); // Act Action action = () => container.Register <ITimeProvider, RealTimeProvider>(); // Assert AssertThat.Throws <InvalidOperationException>( action, "Container is expected to get locked; even when ContainerLocking throws an exception."); }
public void GetService_RequestingANonregisteredType_WillNotSuppressErrorsThrownFromUnregisteredTypeResolution() { // Arrange var container = ContainerFactory.New(); // Registration of an event that registers an invalid delegate, should make GetService fail. container.ResolveUnregisteredType += (sender, e) => { if (e.UnregisteredServiceType == typeof(IUserRepository)) { Func <object> invalidDelegate = () => null; e.Register(invalidDelegate); } }; IServiceProvider serviceProvider = container; // Act Action action = () => serviceProvider.GetService(typeof(IUserRepository)); // Assert AssertThat.Throws <ActivationException>(action); }
private static void _ <T>(Type type, Type genericType) where T : Exception => AssertThat.Throws <T>(() => type.GetClosedTypesOf(genericType));