/src/UnitTests/IOC/MethodInjectionTests.cs

http://github.com/philiplaureano/LinFu · C# · 32 lines · 24 code · 5 blank · 3 comment · 0 complexity · 430472b82859cfd8d7e97fbf318c18b6 MD5 · raw file

  1. using System;
  2. using LinFu.IoC;
  3. using Xunit;
  4. using SampleLibrary;
  5. using SampleLibrary.IOC;
  6. namespace LinFu.UnitTests.IOC
  7. {
  8. public class MethodInjectionTests : BaseTestFixture
  9. {
  10. [Fact]
  11. public void ShouldAutoInjectMethod()
  12. {
  13. var container = new ServiceContainer();
  14. container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll");
  15. var instance = new SampleClassWithInjectionMethod();
  16. // Initialize the container
  17. container.Inject<ISampleService>().Using<SampleClass>().OncePerRequest();
  18. container.Inject<ISampleService>("MyService").Using(c => instance).OncePerRequest();
  19. var result = container.GetService<ISampleService>("MyService");
  20. Assert.Same(result, instance);
  21. // On initialization, the instance.Property value
  22. // should be a SampleClass type
  23. Assert.NotNull(instance.Property);
  24. Assert.Equal(typeof(SampleClass), instance.Property?.GetType());
  25. }
  26. }
  27. }