public void CanReturnServiceIfInitializedAndRegistered() { ServiceContainer container = new ServiceContainer(); container.LoadFromBaseDirectory("Shaml.Data.dll"); container.AddService("validator", typeof(IValidator), typeof(Validator), LinFu.IoC.Configuration.LifecycleType.OncePerRequest); ServiceLocator.SetLocatorProvider(() => new LinFuServiceLocator(container)); IValidator validatorService = SafeServiceLocator<IValidator>.GetService(); Assert.That(validatorService, Is.Not.Null); }
public void ShouldFindGenericMethod() { var container = new ServiceContainer(); container.LoadFromBaseDirectory("*.dll"); var context = new MethodFinderContext(new Type[]{typeof(object)}, new object[0], typeof(void)); var methods = typeof(SampleClassWithGenericMethod).GetMethods(BindingFlags.Public | BindingFlags.Instance); var finder = container.GetService<IMethodFinder<MethodInfo>>(); var result = finder.GetBestMatch(methods, context); Assert.IsTrue(result.IsGenericMethod); Assert.IsTrue(result.GetGenericArguments().Count() == 1); }
public void ShouldAutoInjectClassCreatedWithAutoCreate() { // Configure the container var container = new ServiceContainer(); container.LoadFromBaseDirectory("*.dll"); var sampleService = new Mock<ISampleService>(); container.AddService(sampleService.Object); var instance = (SampleClassWithInjectionProperties)container.AutoCreate(typeof(SampleClassWithInjectionProperties)); // The container should initialize the SomeProperty method to match the mock ISampleService instance Assert.IsNotNull(instance.SomeProperty); Assert.AreSame(instance.SomeProperty, sampleService.Object); }
public void ShouldBeAbleToRedirectInterfaceCallToTarget() { var container = new ServiceContainer(); container.LoadFromBaseDirectory("*.dll"); // The duck should call the implementation instance var mock = new Mock<SampleDuckTypeImplementation>(); mock.Expect(i => i.DoSomething()); object target = mock.Object; var sampleService = target.CreateDuck<ISampleService>(); sampleService.DoSomething(); mock.VerifyAll(); }
public void ShouldBeAbleToInstantiateCustomFactoryWithServiceArgumentsInConstructor() { var mock = new Mock<ISampleService>(); var container = new ServiceContainer(); container.LoadFromBaseDirectory("*.dll"); container.AddService(mock.Object); var result = container.GetService<string>("SampleFactoryWithConstructorArguments"); Assert.IsNotNull(result); Assert.IsNotEmpty(result); }