public void ResolveCreatesSingletonIfSpecified() { var iocContainer = new MicroIocContainer() .Register <IFoo, Foo>(isSingleton: true); var instance1 = iocContainer.Resolve <IFoo>(); var instance2 = iocContainer.Resolve <IFoo>(); Assert.AreSame(instance1, instance2); }
public void ResolveCreatesInstancePerRequest() { var iocContainer = new MicroIocContainer() .Register <IFoo, Foo>(); var instance1 = iocContainer.Resolve <IFoo>(); var instance2 = iocContainer.Resolve <IFoo>(); Assert.AreNotSame(instance1, instance2); }
public void RegisteringSubsequentImplentationTypesOverwritesPrevious() { var iocContainer = new MicroIocContainer() .Register <IFoo, Foo>(); Assert.IsInstanceOfType(iocContainer.Resolve <IFoo>(), typeof(Foo)); iocContainer.Register <IFoo, Foo2>(); Assert.IsInstanceOfType(iocContainer.Resolve <IFoo>(), typeof(Foo2)); }
public void RegisteringSubsequentInstancesOfTypeWithSameKeyOverwritesPrevious() { const string key = "fooKey"; // Nell var foo1 = new Foo(); var foo2 = new Foo(); var iocContainer = new MicroIocContainer() .RegisterInstance <IFoo>(foo1, key); Assert.AreSame(foo1, iocContainer.Resolve <IFoo>(key)); iocContainer.RegisterInstance <IFoo>(foo2, key); Assert.AreSame(foo2, iocContainer.Resolve <IFoo>(key)); }
public void ContainerCanResolveUnregisteredConcreteTypes() { var iocContainer = new MicroIocContainer(); var instance = iocContainer.Resolve <Foo>(); Assert.IsNotNull(instance); Assert.IsInstanceOfType(instance, typeof(Foo)); }
public void ResoveCreatesItemOfRequiredType() { var iocContainer = new MicroIocContainer() .Register <IFoo, Foo>(); var instance = iocContainer.Resolve <IFoo>(); Assert.IsInstanceOfType(instance, typeof(Foo)); }
public void ContainerRetrievesInstancesByKeyWithSingleParameterTypeRegistration() { var iocContainer = new MicroIocContainer() .Register <Foo2>("Foo2", true); var instance = iocContainer.Resolve(null, "Foo2"); Assert.IsInstanceOfType(instance, typeof(Foo2)); Assert.IsNotNull(instance); }
public void ContainerRetrievesInstancesByKey() { var iocContainer = new MicroIocContainer() .Register <IFoo, Foo>("Foo", true); var instance = iocContainer.Resolve(null, "Foo"); Assert.IsInstanceOfType(instance, typeof(Foo)); Assert.IsNotNull(instance); }
public void ContainerWillChainDependencyInjection() { var iocContainer = new MicroIocContainer() .Register <IFoo, Foo>(); var instance = iocContainer.Resolve <Bar>(); Assert.IsNotNull(instance); Assert.IsNotNull(instance.Foo); }
public void ContainerCanResolveAConcreteTypeWithoutDefaultConstructor() { var iocContainer = new MicroIocContainer() .Register <IFoo, Foo>(); var instance = iocContainer.Resolve <IFoo>(); Assert.IsNotNull(instance); Assert.IsInstanceOfType(instance, typeof(Foo)); }
public void InjectionWorksOnProperties() { const string expectedName = "Fabrikam inc."; var iocContainer = new MicroIocContainer() .RegisterInstance(expectedName, "MicroIoc.Tests.TestClassWithProperty.CustomerName"); var instance = iocContainer.Resolve <TestClassWithProperty>(); Assert.AreEqual(expectedName, instance.CustomerName); }
public void ContainerWillResolveSpecificInstancesInChainedResolution() { var foo1 = new Foo(); var iocContainer = new MicroIocContainer() .RegisterInstance <IFoo>(foo1); var instance = iocContainer.Resolve <Bar>(); Assert.IsNotNull(instance); Assert.AreSame(foo1, instance.Foo); }
public void PropertyInjectionThrowsExceptionForUnregisteredDependency() { const string expectedName = "Fabrikam inc."; var iocContainer = new MicroIocContainer() .RegisterInstance(expectedName) .Register <IFirst, First>(); // Do not register this one... // iocContainer.Register<ISecond, Second>(); iocContainer.Resolve <TestClassWithProperties>(); }
public void ContainerInstantiatesConstructorWithMostParameters() { var iocContainer = new MicroIocContainer() .Register <IFirst, First>() .Register <ISecond, Second>() .Register <IThird, Third>(); var instance = iocContainer.Resolve <OverloadedTestClass>(); Assert.IsNotNull(instance.First); Assert.IsNotNull(instance.Second); Assert.IsNotNull(instance.Third); }
public void ConfiguredConstructorParameterInjectionGetsResolved() { const string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"; var container = new MicroIocContainer(); container.GetConfiguration() .ConstructorParam <SampleRepository, string>("connectionString", connectionString); var instance = container.Resolve <SampleRepository>(); Assert.AreEqual(connectionString, instance.ConnectionString); }
public void ResolveByKeyGetsCorrectInstance() { var foo1 = new Foo(); var foo2 = new Foo(); var iocContainer = new MicroIocContainer() .RegisterInstance <IFoo>(foo1) .RegisterInstance <IFoo>(foo2, "theKey"); var instance = iocContainer.Resolve <IFoo>("theKey"); Assert.AreNotSame(instance, foo1); Assert.AreSame(instance, foo2); }
public void ConfiguredPropertyInjectionGetsResolved() { const string customerName = "Bloggs & Co"; var container = new MicroIocContainer() .Register <IFirst, First>() .Register <ISecond, Second>(); container.GetConfiguration() .Property <TestClassWithProperties, string>(c => c.CustomerName, customerName); var instance = container.Resolve <TestClassWithProperties>(); Assert.AreEqual(customerName, instance.CustomerName); }
public void ContainerResolvesConfiguredPropertyIfSpecified() { var first = new First(); var container = new MicroIocContainer() .Register <IFirst, First>() .Register <ISecond, Second>(); container.GetConfiguration() .Configure <TestClassWithProperties>(new InjectedProperty <string>("CustomerName", "Anything")) .Configure <TestClassWithProperties>(new InjectedProperty <First>("FirstProperty", first)); container.GetConfiguration() .Configure <TestClassWithPropertiesAndConstructor>(new InjectedProperty <string>("CustomerName", "Whatever")); var instance = container.Resolve <TestClassWithProperties>(); Assert.AreSame(first, instance.FirstProperty); var otherInstance = container.Resolve <TestClassWithPropertiesAndConstructor>(); Assert.AreNotSame(first, otherInstance.FirstProperty); }
public void ContainerHandlesMultipleInjectionMethods() { const string expectedName = "Fabrikam inc."; var iocContainer = new MicroIocContainer() .RegisterInstance(expectedName) .Register <IFirst, First>() .Register <ISecond, Second>(); var instance = iocContainer.Resolve <TestClassWithPropertiesAndConstructor>(); Assert.AreEqual(expectedName, instance.CustomerName); Assert.IsInstanceOfType(instance.First, typeof(First)); Assert.IsInstanceOfType(instance.Second, typeof(Second)); }
public void RegisterAllViewModelsCreatesRegistrationByNameForVmsInAssembly() { var iocContainer = new MicroIocContainer() .RegisterAllViewModels(); var threw = false; try { var foo = iocContainer.Resolve(null, "FooViewModel"); Assert.IsInstanceOfType(foo, typeof(FooViewModel)); var bar = iocContainer.Resolve(null, "BarViewModel"); Assert.IsInstanceOfType(bar, typeof(BarViewModel)); } catch (Exception) { threw = true; } finally { Assert.IsFalse(threw); } }
public void RegisterAllViewModelsDoesNotRegisterUnknownVms() { var iocContainer = new MicroIocContainer() .RegisterAllViewModels(); var threw = false; try { // There is no such object - this should throw a ResolutionException var foo = iocContainer.Resolve(null, "BazViewModel"); } catch (ResolutionException) { threw = true; } finally { Assert.IsTrue(threw); } }
public void RegisteringSubsequentInstancesOfTypeWithSameKeyOverwritesPrevious() { const string key = "fooKey"; // Nell var foo1 = new Foo(); var foo2 = new Foo(); var iocContainer = new MicroIocContainer() .RegisterInstance<IFoo>(foo1, key); Assert.AreSame(foo1, iocContainer.Resolve<IFoo>(key)); iocContainer.RegisterInstance<IFoo>(foo2, key); Assert.AreSame(foo2, iocContainer.Resolve<IFoo>(key)); }
public void RegisteringSubsequentImplentationTypesOverwritesPrevious() { var iocContainer = new MicroIocContainer() .Register<IFoo, Foo>(); Assert.IsInstanceOfType(iocContainer.Resolve<IFoo>(), typeof(Foo)); iocContainer.Register<IFoo, Foo2>(); Assert.IsInstanceOfType(iocContainer.Resolve<IFoo>(), typeof(Foo2)); }
public void RegisterAllViewModelsDoesNotRegisterUnknownVms() { var iocContainer = new MicroIocContainer() .RegisterAllViewModels(); var threw = false; try { // There is no such object - this should throw a ResolutionException var foo = iocContainer.Resolve(null, "BazViewModel"); } catch (ResolutionException) { threw = true; } finally { Assert.IsTrue(threw); } }
public void RegisterAllViewModelsCreatesRegistrationByNameForVmsInAssembly() { var iocContainer = new MicroIocContainer() .RegisterAllViewModels(); var threw = false; try { var foo = iocContainer.Resolve(null, "FooViewModel"); Assert.IsInstanceOfType(foo, typeof(FooViewModel)); var bar = iocContainer.Resolve(null, "BarViewModel"); Assert.IsInstanceOfType(bar, typeof(BarViewModel)); } catch (Exception) { threw = true; } finally { Assert.IsFalse(threw); } }
public void PropertyInjectionThrowsExceptionForUnregisteredDependency() { const string expectedName = "Fabrikam inc."; var iocContainer = new MicroIocContainer() .RegisterInstance(expectedName) .Register<IFirst, First>(); // Do not register this one... // iocContainer.Register<ISecond, Second>(); iocContainer.Resolve<TestClassWithProperties>(); }
public void ContainerCanResolveAConcreteTypeWithoutDefaultConstructor() { var iocContainer = new MicroIocContainer() .Register<IFoo, Foo>(); var instance = iocContainer.Resolve<IFoo>(); Assert.IsNotNull(instance); Assert.IsInstanceOfType(instance, typeof (Foo)); }
public void ResolveCreatesSingletonIfSpecified() { var iocContainer = new MicroIocContainer() .Register<IFoo, Foo>(isSingleton: true); var instance1 = iocContainer.Resolve<IFoo>(); var instance2 = iocContainer.Resolve<IFoo>(); Assert.AreSame(instance1, instance2); }
public void ContainerWillChainDependencyInjection() { var iocContainer = new MicroIocContainer() .Register<IFoo, Foo>(); var instance = iocContainer.Resolve<Bar>(); Assert.IsNotNull(instance); Assert.IsNotNull(instance.Foo); }
public void ContainerRetrievesInstancesByKeyWithSingleParameterTypeRegistration() { var iocContainer = new MicroIocContainer() .Register<Foo2>("Foo2", true); var instance = iocContainer.Resolve(null, "Foo2"); Assert.IsInstanceOfType(instance, typeof (Foo2)); Assert.IsNotNull(instance); }
public void ContainerRetrievesInstancesByKey() { var iocContainer = new MicroIocContainer() .Register<IFoo, Foo>("Foo", true); var instance = iocContainer.Resolve(null, "Foo"); Assert.IsInstanceOfType(instance, typeof(Foo)); Assert.IsNotNull(instance); }
public void ContainerInstantiatesConstructorWithMostParameters() { var iocContainer = new MicroIocContainer() .Register<IFirst, First>() .Register<ISecond, Second>() .Register<IThird, Third>(); var instance = iocContainer.Resolve<OverloadedTestClass>(); Assert.IsNotNull(instance.First); Assert.IsNotNull(instance.Second); Assert.IsNotNull(instance.Third); }
public void ContainerHandlesMultipleProperties() { const string expectedName = "Fabrikam inc."; var iocContainer = new MicroIocContainer() .RegisterInstance(expectedName, "MicroIoc.Tests.TestClassWithProperties.CustomerName") .Register<IFirst, First>() .Register<ISecond, Second>(); var instance = iocContainer.Resolve<TestClassWithProperties>(); Assert.AreEqual(expectedName, instance.CustomerName); Assert.IsInstanceOfType(instance.FirstProperty, typeof(First)); Assert.IsInstanceOfType(instance.SecondProperty, typeof(Second)); }
public void ContainerCanResolveUnregisteredConcreteTypes() { var iocContainer = new MicroIocContainer(); var instance = iocContainer.Resolve<Foo>(); Assert.IsNotNull(instance); Assert.IsInstanceOfType(instance, typeof(Foo)); }
public void ResolveByKeyGetsCorrectInstance() { var foo1 = new Foo(); var foo2 = new Foo(); var iocContainer = new MicroIocContainer() .RegisterInstance<IFoo>(foo1) .RegisterInstance<IFoo>(foo2, "theKey"); var instance = iocContainer.Resolve<IFoo>("theKey"); Assert.AreNotSame(instance, foo1); Assert.AreSame(instance, foo2); }
public void ResolveCreatesInstancePerRequest() { var iocContainer = new MicroIocContainer() .Register<IFoo, Foo>(); var instance1 = iocContainer.Resolve<IFoo>(); var instance2 = iocContainer.Resolve<IFoo>(); Assert.AreNotSame(instance1, instance2); }
public void ContainerWillResolveSpecificInstancesInChainedResolution() { var foo1 = new Foo(); var iocContainer = new MicroIocContainer() .RegisterInstance<IFoo>(foo1); var instance = iocContainer.Resolve<Bar>(); Assert.IsNotNull(instance); Assert.AreSame(foo1, instance.Foo); }
public void ResoveCreatesItemOfRequiredType() { var iocContainer = new MicroIocContainer() .Register<IFoo, Foo>(); var instance = iocContainer.Resolve<IFoo>(); Assert.IsInstanceOfType(instance, typeof(Foo)); }
public void InjectionWorksOnProperties() { const string expectedName = "Fabrikam inc."; var iocContainer = new MicroIocContainer() .RegisterInstance(expectedName, "MicroIoc.Tests.TestClassWithProperty.CustomerName"); var instance = iocContainer.Resolve<TestClassWithProperty>(); Assert.AreEqual(expectedName, instance.CustomerName); }