/src/UnitTests/Proxy/LazyObjectTests.cs

http://github.com/philiplaureano/LinFu · C# · 38 lines · 31 code · 5 blank · 2 comment · 0 complexity · 8014e5c894b7fa601a1f8b9b218a838d MD5 · raw file

  1. using System.Reflection;
  2. using LinFu.IoC;
  3. using LinFu.IoC.Configuration;
  4. using LinFu.IoC.Configuration.Interfaces;
  5. using LinFu.IoC.Interceptors;
  6. using LinFu.Proxy;
  7. using LinFu.Proxy.Interfaces;
  8. using Xunit;
  9. using SampleLibrary;
  10. using SampleLibrary.IOC;
  11. namespace LinFu.UnitTests.Proxy
  12. {
  13. public class LazyObjectTests : BaseTestFixture
  14. {
  15. [Fact]
  16. public void LazyObjectShouldNeverBeInitialized()
  17. {
  18. var container = new ServiceContainer();
  19. container.AddService<IProxyFactory>(new ProxyFactory());
  20. container.AddService<IMethodBuilder<MethodInfo>>(new MethodBuilder());
  21. Assert.True(container.Contains(typeof(IProxyFactory)));
  22. var proxyFactory = container.GetService<IProxyFactory>();
  23. var interceptor = new LazyInterceptor<ISampleService>(() => new SampleLazyService());
  24. SampleLazyService.Reset();
  25. // The instance should be uninitialized at this point
  26. var proxy = proxyFactory.CreateProxy<ISampleService>(interceptor);
  27. Assert.False(SampleLazyService.IsInitialized);
  28. // The instance should be initialized once the method is called
  29. proxy.DoSomething();
  30. Assert.True(SampleLazyService.IsInitialized);
  31. }
  32. }
  33. }