public void WhenGettingServiceWithOverrideThenReturnsOverride() { ProcessorConfiguration config = new ProcessorConfiguration(); HandlerServices services = new HandlerServices(config.Services); ICommandHandlerTypeResolver newLocalService = new Mock<ICommandHandlerTypeResolver>().Object; services.Replace(typeof(ICommandHandlerTypeResolver), newLocalService); // Act ICommandHandlerTypeResolver localVal = (ICommandHandlerTypeResolver)services.GetService(typeof(ICommandHandlerTypeResolver)); ICommandHandlerTypeResolver globalVal = (ICommandHandlerTypeResolver)config.Services.GetService(typeof(ICommandHandlerTypeResolver)); // Assert // Local handler didn't override, should get same value as global case. Assert.Same(localVal, newLocalService); Assert.NotSame(localVal, globalVal); }
public void WhenGettingServiceWithDependencyInjectionThenReturnsFromDependencyInjection() { // Setting on Handler config overrides the DI container. ProcessorConfiguration config = new ProcessorConfiguration(); ICommandHandlerTypeResolver newDiService = new Mock<ICommandHandlerTypeResolver>().Object; var mockDependencyResolver = new Mock<IDependencyResolver>(); mockDependencyResolver.Setup(dr => dr.GetService(typeof(ICommandHandlerTypeResolver))).Returns(newDiService); config.DependencyResolver = mockDependencyResolver.Object; HandlerServices services = new HandlerServices(config.Services); ICommandHandlerTypeResolver newLocalService = new Mock<ICommandHandlerTypeResolver>().Object; services.Replace(typeof(ICommandHandlerTypeResolver), newLocalService); // Act ICommandHandlerTypeResolver localVal = (ICommandHandlerTypeResolver)services.GetService(typeof(ICommandHandlerTypeResolver)); ICommandHandlerTypeResolver globalVal = (ICommandHandlerTypeResolver)config.Services.GetService(typeof(ICommandHandlerTypeResolver)); // Assert // Local handler didn't override, should get same value as global case. Assert.Same(newDiService, globalVal); // asking the config will give back the DI service Assert.Same(newLocalService, localVal); // but asking locally will get back the local service. }
public void WhenSettingOverrideToNullThenReturnsNull() { ProcessorConfiguration config = new ProcessorConfiguration(); ServicesContainer global = config.Services; HandlerServices services = new HandlerServices(global); // Act // Setting to null is not the same as clear. Clear() means fall through to global config. services.Replace(typeof(ICommandHandlerTypeResolver), null); // Assert ICommandHandlerTypeResolver localVal = (ICommandHandlerTypeResolver)services.GetService(typeof(ICommandHandlerTypeResolver)); Assert.Null(localVal); }
public void WhenClearingOverridedServiceThenReturnsOriginal() { ProcessorConfiguration config = new ProcessorConfiguration(); ServicesContainer global = config.Services; HandlerServices services = new HandlerServices(global); ICommandHandlerTypeResolver newLocalService = new Mock<ICommandHandlerTypeResolver>().Object; services.Replace(typeof(ICommandHandlerTypeResolver), newLocalService); // Act services.Clear(typeof(ICommandHandlerTypeResolver)); // Assert ICommandHandlerTypeResolver localVal = (ICommandHandlerTypeResolver)services.GetService(typeof(ICommandHandlerTypeResolver)); ICommandHandlerTypeResolver globalVal = (ICommandHandlerTypeResolver)global.GetService(typeof(ICommandHandlerTypeResolver)); Assert.Same(globalVal, localVal); }