PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Machine.Specifications.Specs/Runner/AppDomainRunnerSpecs.cs

https://github.com/machine/machine.specifications
C# | 184 lines | 145 code | 39 blank | 0 comment | 1 complexity | 33bad7cddd4fcc17a743d8dc463939c2 MD5 | raw file
Possible License(s): MIT, CC-BY-SA-3.0
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using Example;
  5. using Example.CleanupFailure;
  6. using Example.Random;
  7. using FluentAssertions;
  8. using Machine.Specifications.Runner;
  9. using Machine.Specifications.Runner.Impl;
  10. namespace Machine.Specifications.Specs.Runner
  11. {
  12. public class running_specs
  13. {
  14. public static TestListener listener;
  15. public static AppDomainRunner runner;
  16. Establish context = () =>
  17. {
  18. listener = new TestListener();
  19. runner = new AppDomainRunner(listener, RunOptions.Default);
  20. };
  21. }
  22. public class when_running_specs_by_assembly : running_specs
  23. {
  24. Because of = () =>
  25. runner.RunAssembly(typeof(Account).Assembly);
  26. It should_run_them_all = () =>
  27. listener.SpecCount.Should().Be(6);
  28. }
  29. public class when_running_specs_in_an_assembly_with_a_reference_that_cannot_be_bound : running_specs
  30. {
  31. static Exception Exception;
  32. readonly static string SpecAssembly = GetPath("Example.BindingFailure.dll");
  33. readonly static string ReferencedAssembly = GetPath("Example.BindingFailure.Ref.dll");
  34. Establish context = () =>
  35. {
  36. if (File.Exists(ReferencedAssembly))
  37. {
  38. File.Delete(ReferencedAssembly);
  39. }
  40. };
  41. Because of = () => runner.RunAssembly(Assembly.LoadFrom(SpecAssembly));
  42. It should_fail =
  43. () => listener.LastFatalError.Should().NotBeNull();
  44. It should_record_that_the_referenced_assembly_could_not_be_found =
  45. () => listener.LastFatalError.FullTypeName.Should().Be(typeof(FileNotFoundException).FullName);
  46. static string GetPath(string path)
  47. {
  48. return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), path);
  49. }
  50. }
  51. [Tags("Issue-146")]
  52. public class when_running_an_assembly_that_does_not_use_mspec : running_specs
  53. {
  54. static Exception Exception;
  55. readonly static string SpecAssembly = GetPath(@"Issue146-NoMSpecDll\Example.Issue146-NoMSpecDll.dll");
  56. Because of = () => runner.RunAssembly(Assembly.LoadFrom(SpecAssembly));
  57. It should_succeed =
  58. () => true.Should().BeTrue();
  59. static string GetPath(string path)
  60. {
  61. return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), path);
  62. }
  63. }
  64. [Ignore("exceptions during cleanup are ignored")]
  65. public class when_running_specs_in_which_the_cleanup_throws_a_non_serializable_exception : running_specs
  66. {
  67. Because of = () =>
  68. runner.RunAssembly(typeof(cleanup_failure).Assembly);
  69. It should_cause_a_fatal_error = () =>
  70. listener.LastFatalError.Should().NotBeNull();
  71. }
  72. public class when_running_specs_by_namespace : running_specs
  73. {
  74. Because of = () =>
  75. runner.RunNamespace(typeof(Account).Assembly, "Example");
  76. It should_run_them_all = () =>
  77. listener.SpecCount.Should().Be(6);
  78. }
  79. public class when_running_specs_by_member : running_specs
  80. {
  81. Because of = () =>
  82. runner.RunMember(typeof(Account).Assembly, typeof(when_transferring_an_amount_larger_than_the_balance_of_the_from_account).GetField("should_not_allow_the_transfer", BindingFlags.NonPublic | BindingFlags.Instance));
  83. It should_run = () =>
  84. listener.SpecCount.Should().Be(1);
  85. }
  86. public class when_running_a_nested_context_by_member : running_specs
  87. {
  88. Because of = () =>
  89. runner.RunMember(typeof(Container).Assembly, typeof(Container.nested_context));
  90. It should_run = () =>
  91. listener.SpecCount.Should().Be(1);
  92. }
  93. public class when_running_specs_of_a_nested_context_by_member : running_specs
  94. {
  95. Because of = () =>
  96. runner.RunMember(typeof(Container).Assembly, typeof(Container.nested_context).GetField("should_be_run", BindingFlags.NonPublic | BindingFlags.Instance));
  97. It should_run = () =>
  98. listener.SpecCount.Should().Be(1);
  99. }
  100. public class TestListener : ISpecificationRunListener
  101. {
  102. public int SpecCount;
  103. public AssemblyInfo LastAssembly { get; private set; }
  104. public ContextInfo LastContext { get; private set; }
  105. public SpecificationInfo LastSpecification { get; private set; }
  106. public ExceptionResult LastFatalError { get; private set; }
  107. public Result LastResult { get; private set; }
  108. public void OnRunStart()
  109. {
  110. LastAssembly = null;
  111. LastContext = null;
  112. LastResult = null;
  113. }
  114. public void OnRunEnd()
  115. {
  116. }
  117. public void OnAssemblyStart(AssemblyInfo assembly)
  118. {
  119. }
  120. public void OnAssemblyEnd(AssemblyInfo assembly)
  121. {
  122. LastAssembly = assembly;
  123. }
  124. public void OnContextStart(ContextInfo context)
  125. {
  126. }
  127. public void OnContextEnd(ContextInfo context)
  128. {
  129. LastContext = context;
  130. }
  131. public void OnSpecificationStart(SpecificationInfo specification)
  132. {
  133. }
  134. public void OnSpecificationEnd(SpecificationInfo specification, Result result)
  135. {
  136. LastSpecification = specification;
  137. LastResult = result;
  138. SpecCount++;
  139. }
  140. public void OnFatalError(ExceptionResult exception)
  141. {
  142. LastFatalError = exception;
  143. }
  144. }
  145. }