PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

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