public void RegisterSingleByType_RegisteringAnInternalType_ThrowsExpectedException() { // Arrange string expectedMessage = ExpectedSandboxFailureExpectedMessage; var container = new Container(); try { // Act container.Register(typeof(IPublicService), typeof(InternalImplOfPublicService), Lifestyle.Singleton); Assert.Fail("The call is expected to fail inside a Silverlight sandbox."); } catch (ArgumentException ex) { // Assert AssertThat.StringContains(expectedMessage, ex.Message); } }
public void Register_RegisteringANonConcreteType_ThrowsAnArgumentExceptionWithExpectedMessage() { // Arrange string expectedMessage = typeof(IUserRepository).Name + " is not a concrete type."; var container = ContainerFactory.New(); try { // Act container.Register <IUserRepository>(); Assert.Fail("The abstract type was not expected to be registered successfully."); } catch (ArgumentException ex) { AssertThat.IsInstanceOfType(typeof(ArgumentException), ex, "No subtype was expected."); AssertThat.StringContains(expectedMessage, ex.Message); } }
public void GetInstanceNonGeneric_UnregisteredConcreteTypeWithMultiplePublicConstructors_ThrowsExceptionWithNameOfType() { // Arrange var container = ContainerFactory.New(); try { // Act container.GetInstance(typeof(ConcreteTypeWithMultiplePublicConstructors)); // Assert Assert.Fail("Exception was expected."); } catch (ActivationException ex) { string message = ex.Message; AssertThat.StringContains(typeof(ConcreteTypeWithMultiplePublicConstructors).Name, message); } }
public void GetInstance_UnregisteredConcreteTypeWithMultiplePublicConstructors_ThrowsException() { // Arrange var container = ContainerFactory.New(); try { // Act container.GetInstance <ConcreteTypeWithMultiplePublicConstructors>(); // Assert Assert.Fail("Exception was expected."); } catch (ActivationException ex) { string message = ex.Message; AssertThat.StringContains(typeof(ConcreteTypeWithMultiplePublicConstructors).Name, message); AssertThat.StringContains("should contain exactly one public constructor, but it has 2.", message); } }
public void GetInstance_ResolvingAnInternalTypeWithUnregisteredConcreteDependency_FailsWithExpectedExceptionMessage() { // Arrange string expectedMessage = "Explicitly register the type using one of the generic 'Register' " + "overloads or consider making it public."; var container = new Container(); try { // Act container.GetInstance <InternalOuterTypeDependingOnConcrete>(); // Assert Assert.Fail("The call is expected to fail inside a Silverlight sandbox."); } catch (ActivationException ex) { AssertThat.StringContains(expectedMessage, ex.Message); } }
public void RegisterAll_RegisteringAnInternalType_ThrowsExpectedException() { // Arrange string expectedMessage = ExpectedSandboxFailureExpectedMessage; var container = new Container(); IEnumerable instances = new[] { new InternalServiceImpl(null) }; try { // Act container.RegisterCollection(typeof(IInternalService), instances); Assert.Fail("The call is expected to fail inside a Silverlight sandbox."); } catch (ArgumentException ex) { // Assert AssertThat.StringContains(expectedMessage, ex.Message); } }
public void RegisterByFunc_RegisteringAnInternalType_ThrowsExpectedException() { // Arrange string expectedMessage = ExpectedSandboxFailureExpectedMessage; var container = new Container(); Func <object> creator = () => new InternalServiceImpl(null); try { // Act container.Register(typeof(IInternalService), creator); Assert.Fail("The call is expected to fail inside a Silverlight sandbox."); } catch (ArgumentException ex) { // Assert AssertThat.StringContains(expectedMessage, ex.Message); } }
public void Validate_InValidRegisterSingleByFuncRegistration_ThrowsExpectedExceptionMessage() { // Arrange string expectedMessage = "The registered delegate for type IUserRepository returned null"; var container = ContainerFactory.New(); Func <IUserRepository> invalidDelegate = () => null; container.RegisterSingle <IUserRepository>(invalidDelegate); try { // Act container.Verify(); // Arrange Assert.Fail("Exception expected."); } catch (InvalidOperationException ex) { AssertThat.StringContains(expectedMessage, ex.Message); } }
public void GetInstance_ThrowingDelegateRegisteredUsingRegisterSingleByFuncOfNonRootType_ThrowsActivationExceptionWithExpectedExceptionMessage() { // Arrange var container = ContainerFactory.New(); container.RegisterSingle <IUserRepository>(() => { throw new Exception("Bla."); }); try { // Act container.GetInstance <RealUserService>(); // Assert Assert.Fail("The GetInstance method was expected to fail, because of the faulty registration."); } catch (ActivationException ex) { string expectedMessage = "The registered delegate for type IUserRepository threw an exception. Bla."; AssertThat.StringContains(expectedMessage, ex.Message); } }
public void GetInstance_OnConcreteTypeWithValueTypeConstructorArgument_FailsWithExpectedException() { // Arrange string expectedMessage = typeof(ConcreteTypeWithValueTypeConstructorArgument).Name + " contains" + " parameter 'intParam' of type Int32 which can not be used for constructor " + "injection because it is a value type."; var container = ContainerFactory.New(); try { // Act // This type contains constructor with parameter "int intParam". container.GetInstance <ConcreteTypeWithValueTypeConstructorArgument>(); // Assert Assert.Fail("The call was expected to fail."); } catch (ActivationException ex) { AssertThat.StringContains(expectedMessage, ex.Message); } }
public void GetInstance_CalledMultipleTimesOnRegisteredTypeWithFailingDelegate_WillAlwaysFailWithTheSameException() { // Arrange string expectedExceptionMessage = "The registered delegate for type IUserRepository threw an exception."; var container = ContainerFactory.New(); // We let the registered delegate throw an exception. container.Register <IUserRepository>(() => { throw new InvalidOperationException(); }); try { container.GetInstance <IUserRepository>(); Assert.Fail("Test setup fail."); } catch (Exception ex) { AssertThat.StringContains(expectedExceptionMessage, ex.Message, "Test setup failed."); } // Act try { container.GetInstance <IUserRepository>(); Assert.Fail("GetInstance was expected to throw an exception."); } catch (Exception ex) { // Assert AssertThat.StringContains(expectedExceptionMessage, ex.Message, "The GetInstance is " + "expected to always fail with the same exception. When this is not the case, this " + "indicates that recursion detection went off."); } }
public void GetInstance_RequestingTypeDependingOnItself_ThrowsExceptionWithExpectedMessage() { // Arrange string expectedExcpetionMessage = @" The configuration is invalid. The type CyclicDependencyValidatorTests.A is directly or indirectly depending on itself." .TrimInside(); var container = ContainerFactory.New(); try { // Act // Note: A depends on B which depends on A. container.GetInstance <A>(); // Assert Assert.Fail("An exception was expected, because A depends indirectly on itself."); } catch (ActivationException ex) { AssertThat.StringContains(expectedExcpetionMessage, ex.Message); } }
public void Verify_RegisteredCollectionWithNullElements_ThrowsException() { // Arrange var container = ContainerFactory.New(); IEnumerable <IUserRepository> repositories = new IUserRepository[] { null }; container.Collection.Register <IUserRepository>(repositories); try { // Act container.Verify(); // Assert Assert.Fail("Exception expected."); } catch (InvalidOperationException ex) { AssertThat.StringContains( "One of the items in the collection for type IUserRepository is a null reference.", ex.Message); } }
public void GetInstance_MultipleDelegatesHookedUpToEvent_FailsWhenBothDelegatesRegisterSameServiceType() { // Arrange string expectedMessage = "Multiple observers of the ResolveUnregisteredType event are " + "registering a delegate for the same service type"; var container = ContainerFactory.New(); container.ResolveUnregisteredType += (s, e) => e.Register(() => new SqlUserRepository()); container.ResolveUnregisteredType += (s, e) => e.Register(() => new InMemoryUserRepository()); try { // Act container.GetInstance <IUserRepository>(); // Assert Assert.Fail("Exception expected."); } catch (ActivationException ex) { AssertThat.StringContains(expectedMessage, ex.Message); } }
public void GetInstance_WithErrorInNestedDependency_ThrowsExceptionThatContainsAllTypes() { // Arrange var container = ContainerFactory.New(); container.Register <IUserRepository>(() => { throw new InvalidOperationException("Bla."); }); try { // Act container.GetInstance <RealUserService>(); Assert.Fail("This call is expected to fail, because RealUserService depends on IUserRepository."); } catch (ActivationException ex) { AssertThat.StringContains(typeof(RealUserService).Name, ex.Message); // Note: the next line is removed. We optimized Func<T> registrations, and because of this // we miss the information about that type. // AssertThat.StringContains(typeof(IUserRepository).Name, ex.Message); AssertThat.StringContains("Bla", ex.Message); } }