PageRenderTime 128ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/unity/OpenRasta.DI.Unity.Tests.Unit/UnityDependencyResolver_Specification.cs

https://github.com/jbogard/openrasta-stable
C# | 336 lines | 275 code | 61 blank | 0 comment | 1 complexity | 4858b0e11ac4fd8524e2af59c9627e5d MD5 | raw file
  1. using System.Collections;
  2. using System.Linq;
  3. using InternalDependencyResolver_Specification;
  4. using Microsoft.Practices.Unity;
  5. using NUnit.Framework;
  6. using OpenRasta.DI;
  7. using OpenRasta.DI.Unity;
  8. using OpenRasta.DI.Unity.Extensions;
  9. using OpenRasta.Testing;
  10. namespace UnityDependencyResolver_Specification
  11. {
  12. [TestFixture]
  13. public class when_resolving_instances_with_the_unity_resolver : when_resolving_instances
  14. {
  15. public override IDependencyResolver CreateResolver()
  16. {
  17. return new UnityDependencyResolver();
  18. }
  19. [Test]
  20. public void all_registered_dependencies_can_be_resolved()
  21. {
  22. Resolver.AddDependency<IFoo, AFoo>();
  23. Resolver.AddDependency<IFoo, BFoo>();
  24. Resolver.AddDependency<IFoo, FooBar>();
  25. var results = Resolver.ResolveAll<IFoo>();
  26. results.ShouldHaveCountOf(3);
  27. results.ShouldContainInstanceOf<AFoo>();
  28. results.ShouldContainInstanceOf<BFoo>();
  29. results.ShouldContainInstanceOf<FooBar>();
  30. }
  31. [Test]
  32. public void only_matching_dependencies_are_resolved()
  33. {
  34. Resolver.AddDependency<IFoo, AFoo>();
  35. Resolver.AddDependency<IBar, ABar>();
  36. var results = Resolver.ResolveAll<IFoo>();
  37. results.ShouldHaveCountOf(1);
  38. results.ShouldContainInstanceOf<AFoo>();
  39. }
  40. [Test]
  41. public void complex_cycles_generate_an_error()
  42. {
  43. Resolver.AddDependency<TriCycleA>();
  44. Resolver.AddDependency<TriCycleB>();
  45. Resolver.AddDependency<TriCycleC>();
  46. Executing(() => Resolver.Resolve<TriCycleA>())
  47. .ShouldThrow<DependencyResolutionException>();
  48. }
  49. [Test]
  50. public void dependencies_used_multiple_times_do_not_cause_cyclic_errors()
  51. {
  52. Resolver.AddDependency<IFoo, AFoo>();
  53. Resolver.AddDependency<ComplexChild>();
  54. Resolver.AddDependency<ComplexParent>();
  55. Resolver.Resolve<ComplexParent>()
  56. .ShouldNotBeNull();
  57. }
  58. [Test]
  59. public void unregistered_nested_dependencies_cause_an_error()
  60. {
  61. Resolver.AddDependency<IFoo, AFoo>();
  62. Resolver.AddDependency<ComplexParent>();
  63. Executing(() => Resolver.Resolve<ComplexParent>())
  64. .ShouldThrow<DependencyResolutionException>();
  65. }
  66. [Test]
  67. public void can_resolve_multiple_times()
  68. {
  69. Resolver.AddDependency<AFoo>();
  70. Resolver.Resolve<AFoo>().ShouldNotBeNull();
  71. Resolver.Resolve<AFoo>().ShouldNotBeNull();
  72. Resolver.Resolve<AFoo>().ShouldNotBeNull();
  73. }
  74. [Test]
  75. public void explicit_injection_constructors_are_used()
  76. {
  77. Resolver.AddDependency<IFoo, AFoo>();
  78. Resolver.AddDependency<IBar, ABar>();
  79. Resolver.AddDependency<MarkedConstructor>();
  80. Resolver.Resolve<MarkedConstructor>().Constructor.ShouldBe(2);
  81. }
  82. }
  83. public static class MoreSpecExtensions
  84. {
  85. public static bool ShouldContainInstanceOf<TService>(this IEnumerable instances)
  86. {
  87. return instances
  88. .AsQueryable()
  89. .OfType<object>()
  90. .Any(x => x.GetType() == typeof(TService));
  91. }
  92. }
  93. [TestFixture]
  94. public class when_registering_dependencies_with_the_unity_resolver : when_registering_dependencies
  95. {
  96. public override IDependencyResolver CreateResolver()
  97. {
  98. return new UnityDependencyResolver();
  99. }
  100. [Test]
  101. public void unregistered_dependencies_can_not_be_found()
  102. {
  103. Resolver.HasDependency<IFoo>().ShouldBe(false);
  104. Resolver.HasDependency<AFoo>().ShouldBe(false);
  105. }
  106. [Test]
  107. public void abstract_dependencies_can_be_found_by_either_type()
  108. {
  109. Resolver.AddDependency<IFoo, AFoo>();
  110. Resolver.HasDependency<IFoo>().ShouldBe(true);
  111. Resolver.HasDependency<AFoo>().ShouldBe(true);
  112. }
  113. [Test]
  114. public void concrete_dependencies_can_only_be_found_by_their_concrete_type()
  115. {
  116. Resolver.AddDependency<AFoo>();
  117. Resolver.HasDependency<AFoo>().ShouldBe(true);
  118. Resolver.HasDependency<IFoo>().ShouldBe(false);
  119. }
  120. [Test]
  121. public void multiple_abstract_dependencies_can_resolve_to_the_same_type()
  122. {
  123. Resolver.AddDependency<IFoo, FooBar>();
  124. Resolver.AddDependency<IBar, FooBar>();
  125. Resolver.HasDependency<IFoo>().ShouldBe(true);
  126. Resolver.HasDependency<IFoo>().ShouldBe(true);
  127. }
  128. [Test]
  129. public void only_the_specified_abstract_type_is_registered()
  130. {
  131. Resolver.AddDependency<IFoo, FooBar>();
  132. Resolver.HasDependency<IFoo>().ShouldBe(true);
  133. Resolver.HasDependency<IBar>().ShouldBe(false);
  134. }
  135. }
  136. [TestFixture]
  137. public class when_registering_for_per_request_lifetime_with_unity_dependency_resolver :
  138. when_registering_for_per_request_lifetime
  139. {
  140. public override IDependencyResolver CreateResolver()
  141. {
  142. return new UnityDependencyResolver();
  143. }
  144. }
  145. public class when_using_a_parent_container_with_unity_dependency_resolver : dependency_resolver_context
  146. {
  147. IUnityContainer parent;
  148. public override IDependencyResolver CreateResolver()
  149. {
  150. parent = new UnityContainer();
  151. parent.AddNewExtension<TypeTracker>();
  152. return new UnityDependencyResolver(parent);
  153. }
  154. [Test]
  155. public void dependencies_registered_in_parent_are_available_to_child()
  156. {
  157. parent.RegisterType<IFoo, AFoo>();
  158. Resolver.HasDependency<IFoo>().ShouldBe(true);
  159. }
  160. [Test]
  161. public void dependencies_registered_in_child_are_available_to_child()
  162. {
  163. Resolver.AddDependency<IFoo, AFoo>();
  164. Resolver.HasDependency<IFoo>().ShouldBe(true);
  165. }
  166. [Test]
  167. public void dependencies_registered_in_child_are_not_available_to_parent()
  168. {
  169. Resolver.AddDependency<IFoo, AFoo>();
  170. Executing(() => parent.Resolve<IFoo>())
  171. .ShouldThrow<ResolutionFailedException>();
  172. }
  173. [Test]
  174. public void can_resolve_dependencies_from_both_parent_and_child()
  175. {
  176. parent.RegisterType<IFoo, AFoo>();
  177. Resolver.AddDependency<IFoo, BFoo>();
  178. var results = Resolver.ResolveAll<IFoo>();
  179. results.ShouldHaveCountOf(2);
  180. results.ShouldContainInstanceOf<AFoo>();
  181. results.ShouldContainInstanceOf<BFoo>();
  182. }
  183. [Test]
  184. public void properties_on_types_in_child_container_are_injected()
  185. {
  186. parent.RegisterType<Simple>();
  187. Resolver.AddDependency<WithProperty>();
  188. Resolver.Resolve<WithProperty>().Simple.ShouldNotBeNull();
  189. }
  190. [Test]
  191. public void properties_on_types_in_parent_container_are_not_injected()
  192. {
  193. parent.RegisterType<Simple>();
  194. parent.RegisterType<WithProperty>();
  195. Resolver.Resolve<WithProperty>().Simple.ShouldBeNull();
  196. }
  197. [Test]
  198. public void unregistered_dependencies_cause_error_in_child_container()
  199. {
  200. parent.RegisterType<IFoo, AFoo>();
  201. Resolver.AddDependency<ComplexParent>();
  202. Executing(() => Resolver.Resolve<ComplexParent>())
  203. .ShouldThrow<DependencyResolutionException>();
  204. }
  205. [Test]
  206. public void unregistered_dependencies_from_parent_cause_error_child_container()
  207. {
  208. parent.RegisterType<IFoo, AFoo>();
  209. parent.RegisterType<ComplexParent>();
  210. Executing(() => Resolver.Resolve<ComplexParent>())
  211. .ShouldThrow<DependencyResolutionException>();
  212. }
  213. [Test]
  214. public void unregistered_dependencies_from_parent_are_resolved_from_parent_container()
  215. {
  216. parent.RegisterType<IFoo, AFoo>();
  217. parent.RegisterType<ComplexParent>();
  218. parent.Resolve<ComplexParent>().ShouldNotBeNull();
  219. }
  220. [Test]
  221. public void parent_container_uses_longest_constructor()
  222. {
  223. parent.RegisterType<IFoo, AFoo>();
  224. parent.RegisterType<MultiConstructor>();
  225. Executing(() => parent.Resolve<MultiConstructor>())
  226. .ShouldThrow<ResolutionFailedException>();
  227. }
  228. [Test]
  229. public void child_container_uses_greediest_constructor()
  230. {
  231. parent.RegisterType<IFoo, AFoo>();
  232. Resolver.AddDependency<MultiConstructor>();
  233. Resolver.Resolve<MultiConstructor>().Dependencies.ShouldBe(1);
  234. }
  235. }
  236. public class TriCycleA { public TriCycleA(TriCycleB b) { } }
  237. public class TriCycleB { public TriCycleB(TriCycleC c) { } }
  238. public class TriCycleC { public TriCycleC(TriCycleA a) { } }
  239. public interface IFoo { }
  240. public interface IBar { }
  241. public class AFoo : IFoo { }
  242. public class BFoo : IFoo { }
  243. public class ABar : IBar { }
  244. public class BBar : IBar { }
  245. public class FooBar : IFoo, IBar { }
  246. public class ComplexChild { public ComplexChild(IFoo foo) { } }
  247. public class ComplexParent { public ComplexParent(ComplexChild a, IFoo foo) { } }
  248. public class Simple { }
  249. public class WithProperty { public Simple Simple { get; set; } }
  250. public class MultiConstructor
  251. {
  252. public int Dependencies { get; set; }
  253. public MultiConstructor(IFoo foo, IBar bar)
  254. {
  255. Dependencies = 2;
  256. }
  257. public MultiConstructor(IFoo foo)
  258. {
  259. Dependencies = 1;
  260. }
  261. }
  262. public class MarkedConstructor
  263. {
  264. public int Constructor { get; set; }
  265. public MarkedConstructor(IFoo foo, IBar bar)
  266. {
  267. Constructor = 1;
  268. }
  269. [InjectionConstructor]
  270. public MarkedConstructor(IBar bar, IFoo foo)
  271. {
  272. Constructor = 2;
  273. }
  274. }
  275. }