public void DisposeTest() { SingletonPerScopeLifestyle container = new SingletonPerScopeLifestyle(); FauxInjectionScope fauxInjectionScope = new FauxInjectionScope(); bool eventFired = false; object locatedObject = container.Locate((scope, context) => { DisposableService disposableService = new DisposableService(); scope.AddDisposable(disposableService); disposableService.Disposing += (sender, args) => eventFired = true; return disposableService; }, fauxInjectionScope, new FauxInjectionContext { RequestingScope = fauxInjectionScope }, new FauxExportStrategy(() => new object())); Assert.NotNull(locatedObject); container.Dispose(); Assert.False(eventFired); fauxInjectionScope.Dispose(); Assert.True(eventFired); }
public void DisposalTest() { FauxInjectionScope injectionScope = new FauxInjectionScope(); CompiledExportDelegateInfo info = new CompiledExportDelegateInfo { ActivationType = typeof(DisposableService), IsTransient = true, TrackDisposable = true, Attributes = new Attribute[0] }; FuncCompiledExportDelegate compiledExport = new FuncCompiledExportDelegate(info, (x, y) => new DisposableService(), null, injectionScope); ExportActivationDelegate activationDelegate = compiledExport.CompileDelegate(); Assert.NotNull(activationDelegate); FauxInjectionContext context = new FauxInjectionContext { DisposalScope = new DisposalScope() }; IDisposableService disposableService = (IDisposableService)activationDelegate(injectionScope, context); bool eventFired = false; disposableService.Disposing += (sender, args) => eventFired = true; context.DisposalScope.Dispose(); Assert.True(eventFired); }
public void ActivateTest() { FauxInjectionScope scope = new FauxInjectionScope(); FauxInjectionContext context = new FauxInjectionContext { RequestingScope = scope }; FuncValueProvider<IBasicService> provider = new FuncValueProvider<IBasicService>(() => new BasicService()); object activated = provider.Activate(scope, context, null, null); Assert.NotNull(activated); Assert.IsType(typeof(BasicService), activated); }
public void ActivateTest() { FauxInjectionScope scope = new FauxInjectionScope(); FauxInjectionContext context = new FauxInjectionContext { RequestingScope = scope }; BasicService basicService = new BasicService(); InstanceStrategy<IBasicService> instanceStrategy = new InstanceStrategy<IBasicService>(basicService); object activatedObject = instanceStrategy.Activate(scope, context, null, null); Assert.True(ReferenceEquals(activatedObject, basicService)); }
public void SharedTest() { SingletonPerScopeLifestyle container = new SingletonPerScopeLifestyle(); FauxInjectionScope requestingScope = new FauxInjectionScope(); IBasicService basicService = (IBasicService) container.Locate((x, y) => new BasicService(), new FauxInjectionScope(), new FauxInjectionContext { RequestingScope = requestingScope }, new FauxExportStrategy(() => 0)); IBasicService testService = (IBasicService) container.Locate((x, y) => new BasicService(), new FauxInjectionScope(), new FauxInjectionContext { RequestingScope = requestingScope }, new FauxExportStrategy(() => 0)); Assert.True(ReferenceEquals(basicService, testService)); }
public void ImportMethodTest() { FauxExportStrategy basicService = new FauxExportStrategy(() => new BasicService()) { ExportTypes = new[] { typeof(IBasicService) } }; FauxInjectionScope injectionScope = new FauxInjectionScope(); injectionScope.AddStrategy(basicService); CompiledExportDelegateInfo info = new CompiledExportDelegateInfo { ActivationType = typeof(ImportMethodService), Attributes = new Attribute[0] }; info.ImportMethod(new ImportMethodInfo { MethodToImport = typeof(ImportMethodService).GetRuntimeMethod("ImportMethod", new[] { typeof(IBasicService) }) }); FuncCompiledExportDelegate compiledExport = new FuncCompiledExportDelegate(info, (x, y) => new ImportMethodService(), null, injectionScope); ExportActivationDelegate activationDelegate = compiledExport.CompileDelegate(); Assert.NotNull(activationDelegate); ImportMethodService propertyService = (ImportMethodService)activationDelegate(injectionScope, new InjectionContext(null, injectionScope)); Assert.NotNull(propertyService); }
public void PropertyImportProviderTest() { FauxInjectionScope injectionScope = new FauxInjectionScope(); CompiledExportDelegateInfo info = new CompiledExportDelegateInfo { ActivationType = typeof(ImportPropertyService), Attributes = new Attribute[0] }; info.ImportProperty(new ImportPropertyInfo { Property = typeof(ImportPropertyService).GetProperty("BasicService"), ValueProvider = new FuncValueProvider<IBasicService>(() => new BasicService()) }); FuncCompiledExportDelegate compiledExport = new FuncCompiledExportDelegate(info, (x, y) => new ImportPropertyService(), null, injectionScope); ExportActivationDelegate activationDelegate = compiledExport.CompileDelegate(); Assert.NotNull(activationDelegate); IImportPropertyService propertyService = (IImportPropertyService)activationDelegate(injectionScope, new InjectionContext(null, injectionScope)); Assert.NotNull(propertyService); Assert.NotNull(propertyService.BasicService); }
public void MissingDisposalScopeTest() { FauxInjectionScope injectionScope = new FauxInjectionScope(); CompiledExportDelegateInfo info = new CompiledExportDelegateInfo { ActivationType = typeof(DisposableService), IsTransient = true, TrackDisposable = true, Attributes = new Attribute[0] }; FuncCompiledExportDelegate compiledExport = new FuncCompiledExportDelegate(info, (x, y) => new DisposableService(), null, injectionScope); ExportActivationDelegate activationDelegate = compiledExport.CompileDelegate(); Assert.NotNull(activationDelegate); FauxInjectionContext context = new FauxInjectionContext(); try { IDisposableService disposableService = (IDisposableService)activationDelegate(injectionScope, context); throw new Exception("Should have thrown a DisposalScopeMissingException"); } catch (DisposalScopeMissingException) { } }
public void ImportPropertyRootTransientTest() { FauxExportStrategy basicService = new FauxExportStrategy(() => new BasicService()) { ExportTypes = new[] { typeof(IBasicService) } }; FauxInjectionScope injectionScope = new FauxInjectionScope(); injectionScope.AddStrategy(basicService); CompiledExportDelegateInfo info = new CompiledExportDelegateInfo { ActivationType = typeof(ImportPropertyService), IsTransient = true, Attributes = new Attribute[0] }; info.ImportProperty(new ImportPropertyInfo { Property = typeof(ImportPropertyService).GetProperty("BasicService") }); FuncCompiledExportDelegate compiledExport = new FuncCompiledExportDelegate(info, (x, y) => new ImportPropertyService(), null, injectionScope); ExportActivationDelegate activationDelegate = compiledExport.CompileDelegate(); Assert.NotNull(activationDelegate); IImportPropertyService propertyService = (IImportPropertyService)activationDelegate(injectionScope, new InjectionContext(null, injectionScope)); Assert.NotNull(propertyService); Assert.NotNull(propertyService.BasicService); }