PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Ninject.Test/Unit/ComponentContainerTests.cs

http://github.com/ninject/ninject
C# | 242 lines | 190 code | 52 blank | 0 comment | 0 complexity | 7ce473174b33ccc9c57fbd16b97a8e5d MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. namespace Ninject.Tests.Unit.ComponentContainerTests
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using FluentAssertions;
  7. using Moq;
  8. using Ninject.Components;
  9. using Ninject.Infrastructure.Disposal;
  10. using Ninject.Infrastructure.Introspection;
  11. using Xunit;
  12. public class ComponentContainerContext
  13. {
  14. protected ComponentContainer container;
  15. protected Mock<IKernel> kernelMock;
  16. public ComponentContainerContext()
  17. {
  18. this.SetUp();
  19. }
  20. public void SetUp()
  21. {
  22. this.container = new ComponentContainer();
  23. this.kernelMock = new Mock<IKernel>();
  24. this.container.KernelConfiguration = this.kernelMock.Object;
  25. }
  26. }
  27. public class WhenInstanceIsCreated
  28. {
  29. private Mock<INinjectSettings> _settingsMock;
  30. private Mock<IExceptionFormatter> _exceptionFormatterMock;
  31. public WhenInstanceIsCreated()
  32. {
  33. _settingsMock = new Mock<INinjectSettings>(MockBehavior.Strict);
  34. _exceptionFormatterMock = new Mock<IExceptionFormatter>();
  35. }
  36. [Fact]
  37. public void DefaultConstructor()
  38. {
  39. var container = new ComponentContainer();
  40. var exceptionFormatter = container.Get<IExceptionFormatter>();
  41. Assert.NotNull(exceptionFormatter);
  42. Assert.Same(exceptionFormatter, container.Get<IExceptionFormatter>());
  43. }
  44. [Fact]
  45. public void Constructor_Settings()
  46. {
  47. var container = new ComponentContainer(_settingsMock.Object);
  48. var exceptionFormatter = container.Get<IExceptionFormatter>();
  49. Assert.NotNull(exceptionFormatter);
  50. Assert.Same(exceptionFormatter, container.Get<IExceptionFormatter>());
  51. }
  52. [Fact]
  53. public void Constructor_SettingsAndExceptionFormatter()
  54. {
  55. var container = new ComponentContainer(_settingsMock.Object, _exceptionFormatterMock.Object);
  56. var exceptionFormatter = container.Get<IExceptionFormatter>();
  57. var settings = container.Get(typeof(INinjectSettings));
  58. Assert.NotNull(exceptionFormatter);
  59. Assert.Same(_exceptionFormatterMock.Object, exceptionFormatter);
  60. Assert.Same(exceptionFormatter, container.Get<IExceptionFormatter>());
  61. Assert.NotNull(settings);
  62. Assert.Same(_settingsMock.Object, settings);
  63. Assert.Same(settings, container.Get(typeof(INinjectSettings)));
  64. }
  65. }
  66. public class WhenGetIsCalled : ComponentContainerContext
  67. {
  68. [Fact]
  69. public void ThrowsExceptionIfNoImplementationRegisteredForService()
  70. {
  71. Assert.Throws<InvalidOperationException>(() => this.container.Get<ITestService>());
  72. }
  73. [Fact]
  74. public void ReturnsInstanceWhenOneImplementationIsRegistered()
  75. {
  76. this.container.Add<ITestService, TestServiceA>();
  77. var service = this.container.Get<ITestService>();
  78. service.Should().NotBeNull();
  79. service.Should().BeOfType<TestServiceA>();
  80. }
  81. [Fact]
  82. public void ReturnsInstanceOfFirstRegisteredImplementation()
  83. {
  84. this.container.Add<ITestService, TestServiceA>();
  85. this.container.Add<ITestService, TestServiceB>();
  86. var service = this.container.Get<ITestService>();
  87. service.Should().NotBeNull();
  88. service.Should().BeOfType<TestServiceA>();
  89. }
  90. [Fact]
  91. public void InjectsEnumeratorOfServicesWhenConstructorArgumentIsIEnumerable()
  92. {
  93. this.container.Add<ITestService, TestServiceA>();
  94. this.container.Add<ITestService, TestServiceB>();
  95. this.container.Add<IAsksForEnumerable, AsksForEnumerable>();
  96. var asks = this.container.Get<IAsksForEnumerable>();
  97. asks.Should().NotBeNull();
  98. asks.SecondService.Should().NotBeNull();
  99. asks.SecondService.Should().BeOfType<TestServiceB>();
  100. }
  101. [Fact]
  102. public void SameInstanceIsReturnedByDefault()
  103. {
  104. this.container.Add<ITestService, TestServiceA>();
  105. var service1 = this.container.Get<ITestService>();
  106. var service2 = this.container.Get<ITestService>();
  107. service1.Should().BeSameAs(service2);
  108. }
  109. [Fact]
  110. public void DifferentInstanceAreReturnedForTransients()
  111. {
  112. this.container.AddTransient<ITestService, TestServiceA>();
  113. var service1 = this.container.Get<ITestService>();
  114. var service2 = this.container.Get<ITestService>();
  115. service1.Should().NotBeSameAs(service2);
  116. }
  117. }
  118. public class WhenGetAllIsCalledOnComponentContainer : ComponentContainerContext
  119. {
  120. [Fact]
  121. public void ReturnsSeriesWithSingleItem()
  122. {
  123. this.container.Add<ITestService, TestServiceA>();
  124. var services = this.container.GetAll<ITestService>().ToList();
  125. services.Should().NotBeNull();
  126. services.Count.Should().Be(1);
  127. services[0].Should().BeOfType<TestServiceA>();
  128. }
  129. [Fact]
  130. public void ReturnsInstanceOfEachRegisteredImplementation()
  131. {
  132. this.container.Add<ITestService, TestServiceA>();
  133. this.container.Add<ITestService, TestServiceB>();
  134. var services = this.container.GetAll<ITestService>().ToList();
  135. services.Should().NotBeNull();
  136. services.Count.Should().Be(2);
  137. services[0].Should().BeOfType<TestServiceA>();
  138. services[1].Should().BeOfType<TestServiceB>();
  139. }
  140. [Fact]
  141. public void ReturnsSameInstanceForTwoCallsForSameService()
  142. {
  143. this.container.Add<ITestService, TestServiceA>();
  144. var service1 = this.container.Get<ITestService>();
  145. var service2 = this.container.Get<ITestService>();
  146. service1.Should().NotBeNull();
  147. service2.Should().NotBeNull();
  148. service1.Should().BeSameAs(service2);
  149. }
  150. }
  151. public class WhenRemoveAllIsCalled : ComponentContainerContext
  152. {
  153. [Fact]
  154. public void RemovesAllMappings()
  155. {
  156. this.container.Add<ITestService, TestServiceA>();
  157. var service1 = this.container.Get<ITestService>();
  158. service1.Should().NotBeNull();
  159. this.container.RemoveAll<ITestService>();
  160. Assert.Throws<InvalidOperationException>(() => this.container.Get<ITestService>());
  161. }
  162. [Fact]
  163. public void DisposesOfAllInstances()
  164. {
  165. this.container.Add<ITestService, TestServiceA>();
  166. this.container.Add<ITestService, TestServiceB>();
  167. var services = this.container.GetAll<ITestService>().ToList();
  168. services.Should().NotBeNull();
  169. services.Count.Should().Be(2);
  170. this.container.RemoveAll<ITestService>();
  171. services[0].IsDisposed.Should().BeTrue();
  172. services[1].IsDisposed.Should().BeTrue();
  173. }
  174. }
  175. public class AsksForEnumerable : NinjectComponent, IAsksForEnumerable
  176. {
  177. public ITestService SecondService { get; set; }
  178. public AsksForEnumerable(IEnumerable<ITestService> services)
  179. {
  180. this.SecondService = services.Skip(1).First();
  181. }
  182. }
  183. public interface IAsksForEnumerable : INinjectComponent
  184. {
  185. ITestService SecondService { get; set; }
  186. }
  187. public class TestServiceA : NinjectComponent, ITestService { }
  188. public class TestServiceB : NinjectComponent, ITestService { }
  189. public interface ITestService : INinjectComponent, IDisposableObject { }
  190. }