/src/UnitTests/IOC/FileWatcherTests.cs

http://github.com/philiplaureano/LinFu · C# · 43 lines · 31 code · 7 blank · 5 comment · 0 complexity · 836943d9b58f9e1bbc868b71e1356205 MD5 · raw file

  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using LinFu.IoC;
  5. using Xunit;
  6. using SampleLibrary;
  7. namespace LinFu.UnitTests.IOC
  8. {
  9. public class FileWatcherTests
  10. {
  11. [Fact]
  12. public void ShouldLoadAssemblyIntoLoaderAtRuntime()
  13. {
  14. var path = Path.Combine(@"..\..\..\SampleFileWatcherLibrary\bin\Debug",
  15. AppDomain.CurrentDomain.BaseDirectory);
  16. var targetFile = "SampleFileWatcherLibrary.dll";
  17. var sourceFileName = Path.Combine(path, targetFile);
  18. var container = new ServiceContainer();
  19. container.AutoLoadFrom(AppDomain.CurrentDomain.BaseDirectory, "dummy.dll");
  20. // There should be nothing loaded at this point since the assembly hasn't
  21. // been copied to the target directory yet
  22. Assert.False(container.Contains(typeof(ISampleService)));
  23. // Copy the assembly to the target directory
  24. // and watch for changes
  25. var targetFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dummy.dll");
  26. File.Copy(sourceFileName, targetFileName, true);
  27. // Give the watcher thread enough time to load the assembly into memory
  28. Thread.Sleep(500);
  29. Assert.True(container.Contains(typeof(ISampleService)));
  30. var instance = container.GetService<ISampleService>();
  31. Assert.NotNull(instance);
  32. var typeName = instance.GetType().Name;
  33. Assert.Equal("SampleFileWatcherServiceClassAddedAtRuntime", typeName);
  34. }
  35. }
  36. }