PageRenderTime 29ms CodeModel.GetById 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Ninject.Tests/Integration/StandardKernelTests.cs

https://github.com/daniyord/ninject
C# | 348 lines | 281 code | 67 blank | 0 comment | 1 complexity | 9b77a88e85ee7ae2c4874fc040b30eec MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Ninject.Tests.Fakes;
  5. using Xunit;
  6. using Xunit.Should;
  7. namespace Ninject.Tests.Integration.StandardKernelTests
  8. {
  9. public class StandardKernelContext
  10. {
  11. protected readonly StandardKernel kernel;
  12. public StandardKernelContext()
  13. {
  14. kernel = new StandardKernel();
  15. }
  16. }
  17. public class WhenGetIsCalledForInterfaceBoundService : StandardKernelContext
  18. {
  19. [Fact]
  20. public void SingleInstanceIsReturnedWhenOneBindingIsRegistered()
  21. {
  22. kernel.Bind<IWeapon>().To<Sword>();
  23. var weapon = kernel.Get<IWeapon>();
  24. weapon.ShouldNotBeNull();
  25. weapon.ShouldBeInstanceOf<Sword>();
  26. }
  27. [Fact]
  28. public void ActivationExceptionThrownWhenMultipleBindingsAreRegistered()
  29. {
  30. kernel.Bind<IWeapon>().To<Sword>();
  31. kernel.Bind<IWeapon>().To<Shuriken>();
  32. Assert.Throws<ActivationException>(() => kernel.Get<IWeapon>());
  33. }
  34. [Fact]
  35. public void DependenciesAreInjectedViaConstructor()
  36. {
  37. kernel.Bind<IWeapon>().To<Sword>();
  38. kernel.Bind<IWarrior>().To<Samurai>();
  39. var warrior = kernel.Get<IWarrior>();
  40. warrior.ShouldNotBeNull();
  41. warrior.ShouldBeInstanceOf<Samurai>();
  42. warrior.Weapon.ShouldNotBeNull();
  43. warrior.Weapon.ShouldBeInstanceOf<Sword>();
  44. }
  45. }
  46. public class WhenGetIsCalledForSelfBoundService : StandardKernelContext
  47. {
  48. [Fact]
  49. public void SingleInstanceIsReturnedWhenOneBindingIsRegistered()
  50. {
  51. kernel.Bind<Sword>().ToSelf();
  52. var weapon = kernel.Get<Sword>();
  53. weapon.ShouldNotBeNull();
  54. weapon.ShouldBeInstanceOf<Sword>();
  55. }
  56. [Fact]
  57. public void DependenciesAreInjectedViaConstructor()
  58. {
  59. kernel.Bind<IWeapon>().To<Sword>();
  60. kernel.Bind<Samurai>().ToSelf();
  61. var samurai = kernel.Get<Samurai>();
  62. samurai.ShouldNotBeNull();
  63. samurai.Weapon.ShouldNotBeNull();
  64. samurai.Weapon.ShouldBeInstanceOf<Sword>();
  65. }
  66. }
  67. public class WhenGetIsCalledForUnboundService : StandardKernelContext
  68. {
  69. [Fact]
  70. public void ImplicitSelfBindingIsRegisteredAndActivated()
  71. {
  72. var weapon = kernel.Get<Sword>();
  73. weapon.ShouldNotBeNull();
  74. weapon.ShouldBeInstanceOf<Sword>();
  75. }
  76. [Fact]
  77. public void ImplicitSelfBindingForGenericTypeIsRegisteredAndActivated()
  78. {
  79. var service = kernel.Get<GenericService<int>>();
  80. service.ShouldNotBeNull();
  81. service.ShouldBeInstanceOf<GenericService<int>>();
  82. }
  83. [Fact]
  84. public void ThrowsExceptionIfAnUnboundInterfaceIsRequested()
  85. {
  86. Assert.Throws<ActivationException>(() => kernel.Get<IWeapon>());
  87. }
  88. [Fact]
  89. public void ThrowsExceptionIfAnUnboundAbstractClassIsRequested()
  90. {
  91. Assert.Throws<ActivationException>(() => kernel.Get<AbstractWeapon>());
  92. }
  93. [Fact]
  94. public void ThrowsExceptionIfAnUnboundValueTypeIsRequested()
  95. {
  96. Assert.Throws<ActivationException>(() => kernel.Get<int>());
  97. }
  98. [Fact]
  99. public void ThrowsExceptionIfAStringIsRequestedWithNoBinding()
  100. {
  101. Assert.Throws<ActivationException>(() => kernel.Get<string>());
  102. }
  103. [Fact]
  104. public void ThrowsExceptionIfAnOpenGenericTypeIsRequested()
  105. {
  106. Assert.Throws<ActivationException>(() => kernel.Get(typeof(IGeneric<>)));
  107. }
  108. }
  109. public class WhenGetIsCalledForGenericServiceRegisteredViaOpenGenericType : StandardKernelContext
  110. {
  111. [Fact]
  112. public void GenericParametersAreInferred()
  113. {
  114. kernel.Bind(typeof(IGeneric<>)).To(typeof(GenericService<>));
  115. var service = kernel.Get<IGeneric<int>>();
  116. service.ShouldNotBeNull();
  117. service.ShouldBeInstanceOf<GenericService<int>>();
  118. }
  119. }
  120. public class WhenTryGetIsCalledForInterfaceBoundService : StandardKernelContext
  121. {
  122. public void SingleInstanceIsReturnedWhenOneBindingIsRegistered()
  123. {
  124. kernel.Bind<IWeapon>().To<Sword>();
  125. var weapon = kernel.TryGet<IWeapon>();
  126. weapon.ShouldNotBeNull();
  127. weapon.ShouldBeInstanceOf<Sword>();
  128. }
  129. public void FirstInstanceIsReturnedWhenMultipleBindingsAreRegistered()
  130. {
  131. kernel.Bind<IWeapon>().To<Sword>();
  132. kernel.Bind<IWeapon>().To<Shuriken>();
  133. var weapon = kernel.TryGet<IWeapon>();
  134. weapon.ShouldNotBeNull();
  135. weapon.ShouldBeInstanceOf<Sword>();
  136. }
  137. }
  138. public class WhenTryGetIsCalledForUnboundService : StandardKernelContext
  139. {
  140. public void ImplicitSelfBindingIsRegisteredAndActivatedIfTypeIsSelfBindable()
  141. {
  142. var weapon = kernel.TryGet<Sword>();
  143. weapon.ShouldNotBeNull();
  144. weapon.ShouldBeInstanceOf<Sword>();
  145. }
  146. public void ReturnsNullIfTypeIsNotSelfBindable()
  147. {
  148. var weapon = kernel.TryGet<IWeapon>();
  149. weapon.ShouldBeNull();
  150. }
  151. }
  152. public class WhenGetAllIsCalledForInterfaceBoundService : StandardKernelContext
  153. {
  154. [Fact]
  155. public void ReturnsSeriesOfItemsInOrderTheyWereBound()
  156. {
  157. kernel.Bind<IWeapon>().To<Sword>();
  158. kernel.Bind<IWeapon>().To<Shuriken>();
  159. var weapons = kernel.GetAll<IWeapon>().ToArray();
  160. weapons.ShouldNotBeNull();
  161. weapons.Length.ShouldBe(2);
  162. weapons[0].ShouldBeInstanceOf<Sword>();
  163. weapons[1].ShouldBeInstanceOf<Shuriken>();
  164. }
  165. [Fact]
  166. public void DoesNotActivateItemsUntilTheEnumeratorRunsOverThem()
  167. {
  168. kernel.Bind<IInitializable>().To<InitializableA>();
  169. kernel.Bind<IInitializable>().To<InitializableB>();
  170. IEnumerable<IInitializable> instances = kernel.GetAll<IInitializable>();
  171. IEnumerator<IInitializable> enumerator = instances.GetEnumerator();
  172. InitializableA.Count.ShouldBe(0);
  173. enumerator.MoveNext();
  174. InitializableA.Count.ShouldBe(1);
  175. InitializableB.Count.ShouldBe(0);
  176. enumerator.MoveNext();
  177. InitializableA.Count.ShouldBe(1);
  178. InitializableB.Count.ShouldBe(1);
  179. }
  180. }
  181. public class WhenGetAllIsCalledForGenericServiceRegisteredViaOpenGenericType : StandardKernelContext
  182. {
  183. [Fact]
  184. public void GenericParametersAreInferred()
  185. {
  186. kernel.Bind(typeof(IGeneric<>)).To(typeof(GenericService<>));
  187. kernel.Bind(typeof(IGeneric<>)).To(typeof(GenericService2<>));
  188. var services = kernel.GetAll<IGeneric<int>>().ToArray();
  189. services.ShouldNotBeNull();
  190. services.Length.ShouldBe(2);
  191. services[0].ShouldBeInstanceOf<GenericService<int>>();
  192. services[1].ShouldBeInstanceOf<GenericService2<int>>();
  193. }
  194. }
  195. public class WhenGetAllIsCalledForUnboundService : StandardKernelContext
  196. {
  197. public void ImplicitSelfBindingIsRegisteredAndActivatedIfTypeIsSelfBindable()
  198. {
  199. var weapons = kernel.GetAll<Sword>().ToArray();
  200. weapons.ShouldNotBeNull();
  201. weapons.Length.ShouldBe(1);
  202. weapons[0].ShouldBeInstanceOf<Sword>();
  203. }
  204. public void ReturnsEmptyEnumerableIfTypeIsNotSelfBindable()
  205. {
  206. var weapons = kernel.GetAll<IWeapon>().ToArray();
  207. weapons.ShouldNotBeNull();
  208. weapons.Length.ShouldBe(0);
  209. }
  210. }
  211. public class WhenGetIsCalledWithConstraints : StandardKernelContext
  212. {
  213. [Fact]
  214. public void ReturnsServiceRegisteredViaBindingWithSpecifiedName()
  215. {
  216. kernel.Bind<IWeapon>().To<Shuriken>();
  217. kernel.Bind<IWeapon>().To<Sword>().Named("sword");
  218. var weapon = kernel.Get<IWeapon>("sword");
  219. weapon.ShouldNotBeNull();
  220. weapon.ShouldBeInstanceOf<Sword>();
  221. }
  222. [Fact]
  223. public void ReturnsServiceRegisteredViaBindingThatMatchesPredicate()
  224. {
  225. kernel.Bind<IWeapon>().To<Shuriken>().WithMetadata("type", "range");
  226. kernel.Bind<IWeapon>().To<Sword>().WithMetadata("type", "melee");
  227. var weapon = kernel.Get<IWeapon>(x => x.Get<string>("type") == "melee");
  228. weapon.ShouldNotBeNull();
  229. weapon.ShouldBeInstanceOf<Sword>();
  230. }
  231. }
  232. public class WhenUnbindIsCalled : StandardKernelContext
  233. {
  234. [Fact]
  235. public void RemovesAllBindingsForService()
  236. {
  237. kernel.Bind<IWeapon>().To<Shuriken>();
  238. kernel.Bind<IWeapon>().To<Sword>();
  239. var bindings = kernel.GetBindings(typeof(IWeapon)).ToArray();
  240. bindings.Length.ShouldBe(2);
  241. kernel.Unbind<IWeapon>();
  242. bindings = kernel.GetBindings(typeof(IWeapon)).ToArray();
  243. bindings.ShouldBeEmpty();
  244. }
  245. }
  246. public class WhenRebindIsCalled : StandardKernelContext
  247. {
  248. [Fact]
  249. public void RemovesAllBindingsForServiceAndReplacesWithSpecifiedBinding()
  250. {
  251. kernel.Bind<IWeapon>().To<Shuriken>();
  252. kernel.Bind<IWeapon>().To<Sword>();
  253. var bindings = kernel.GetBindings(typeof(IWeapon)).ToArray();
  254. bindings.Length.ShouldBe(2);
  255. kernel.Rebind<IWeapon>().To<Sword>();
  256. bindings = kernel.GetBindings(typeof(IWeapon)).ToArray();
  257. bindings.Length.ShouldBe(1);
  258. }
  259. }
  260. public class InitializableA : IInitializable
  261. {
  262. public static int Count = 0;
  263. public void Initialize()
  264. {
  265. Count++;
  266. }
  267. }
  268. public class InitializableB : IInitializable
  269. {
  270. public static int Count = 0;
  271. public void Initialize()
  272. {
  273. Count++;
  274. }
  275. }
  276. public interface IGeneric<T> { }
  277. public class GenericService<T> : IGeneric<T> { }
  278. public class GenericService2<T> : IGeneric<T> { }
  279. public interface IGenericWithConstraints<T> where T : class { }
  280. public class GenericServiceWithConstraints<T> : IGenericWithConstraints<T> where T : class { }
  281. }