public void WhenCompositionServiceProvidesExportProvider_ThenChainsDynamicContextWithIt() { var catalog = new TypeCatalog(typeof(Foo)); var container = new CompositionContainer(catalog); var compositionService = new Mock <INuPatternCompositionService>(); compositionService.Setup(x => x.GetExportedValue <ExportProvider>()).Returns(container); compositionService.Setup(x => x.GetExports <IFoo, IComponentMetadata>()) .Returns(new[] { new Lazy <IFoo, IComponentMetadata>( () => new Foo(), Mocks.Of <IComponentMetadata>().First(m => m.CatalogName == Catalog.DefaultCatalogName && m.Id == "Foo")) }); var binding = new DynamicBinding <IFoo>(new DelegatingCompositionService(compositionService.Object), "Foo"); // Make sure the mocking so far is going good. Assert.True(binding.Evaluate()); using (var context = binding.CreateDynamicContext()) { var bar = new Bar(); context.AddExport <IBar>(bar); var result = binding.Evaluate(context); Assert.True(result); Assert.NotNull(binding.Value.Bar); } }
public void WhenAddedExportExistsOnBase_ThenImportManyGetsBoth() { var catalog = new TypeCatalog(typeof(Foo), typeof(Bar)); var container = new CompositionContainer(catalog); var compositionService = new NuPatternCompositionService(container); var binding = new DynamicBinding <IFoo>(new DelegatingCompositionService(compositionService), "Foo"); Assert.True(binding.Evaluate()); Assert.Equal(1, binding.Value.Bars.Count()); using (var context = binding.CreateDynamicContext()) { var bar = new Bar(); context.AddExport <IBar>(bar); var result = binding.Evaluate(context); Assert.True(result); Assert.Equal(2, binding.Value.Bars.Count()); } }
public void WhenCompositionServiceProvidesExportProvider_ThenChainsDynamicContextWithIt() { var catalog = new TypeCatalog(typeof(Foo)); var container = new CompositionContainer(catalog); var compositionService = new Mock<INuPatternCompositionService>(); compositionService.Setup(x => x.GetExportedValue<ExportProvider>()).Returns(container); compositionService.Setup(x => x.GetExports<IFoo, IComponentMetadata>()) .Returns(new[] { new Lazy<IFoo, IComponentMetadata>( () => new Foo(), Mocks.Of<IComponentMetadata>().First(m => m.CatalogName == Catalog.DefaultCatalogName && m.Id == "Foo")) }); var binding = new DynamicBinding<IFoo>(new DelegatingCompositionService(compositionService.Object), "Foo"); // Make sure the mocking so far is going good. Assert.True(binding.Evaluate()); using (var context = binding.CreateDynamicContext()) { var bar = new Bar(); context.AddExport<IBar>(bar); var result = binding.Evaluate(context); Assert.True(result); Assert.NotNull(binding.Value.Bar); } }
public void WhenAddedExportExistsOnBase_ThenOverridesIt() { var catalog = new TypeCatalog(typeof(Foo), typeof(Bar)); var container = new CompositionContainer(catalog); var compositionService = new NuPatternCompositionService(container); var binding = new DynamicBinding<IFoo>(new DelegatingCompositionService(compositionService), "Foo"); Assert.True(binding.Evaluate()); Assert.NotNull(binding.Value.Bar); using (var context = binding.CreateDynamicContext()) { var bar = new Bar(); context.AddExport<IBar>(bar); var result = binding.Evaluate(context); Assert.True(result); Assert.NotNull(binding.Value.Bar); Assert.Same(bar, binding.Value.Bar); } }