PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Ninject.Tests/Unit/ComponentContainerTests.cs

https://github.com/developingchris/ninject
C# | 163 lines | 130 code | 33 blank | 0 comment | 0 complexity | e322b6f432e12ace328cfb2acb5f7240 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Moq;
  5. using Ninject.Components;
  6. using Ninject.Infrastructure.Disposal;
  7. using Xunit;
  8. using Xunit.Should;
  9. namespace Ninject.Tests.Unit.ComponentContainerTests
  10. {
  11. public class ComponentContainerContext
  12. {
  13. protected readonly ComponentContainer container;
  14. protected readonly Mock<IKernel> kernelMock;
  15. public ComponentContainerContext()
  16. {
  17. container = new ComponentContainer();
  18. kernelMock = new Mock<IKernel>();
  19. container.Kernel = kernelMock.Object;
  20. }
  21. }
  22. public class WhenGetIsCalled : ComponentContainerContext
  23. {
  24. [Fact]
  25. public void ThrowsExceptionIfNoImplementationRegisteredForService()
  26. {
  27. Assert.Throws<InvalidOperationException>(() => container.Get<ITestService>());
  28. }
  29. [Fact]
  30. public void ReturnsInstanceWhenOneImplementationIsRegistered()
  31. {
  32. container.Add<ITestService, TestServiceA>();
  33. var service = container.Get<ITestService>();
  34. service.ShouldNotBeNull();
  35. service.ShouldBeInstanceOf<TestServiceA>();
  36. }
  37. [Fact]
  38. public void ReturnsInstanceOfFirstRegisteredImplementation()
  39. {
  40. container.Add<ITestService, TestServiceA>();
  41. container.Add<ITestService, TestServiceB>();
  42. var service = container.Get<ITestService>();
  43. service.ShouldNotBeNull();
  44. service.ShouldBeInstanceOf<TestServiceA>();
  45. }
  46. [Fact]
  47. public void InjectsEnumeratorOfServicesWhenConstructorArgumentIsIEnumerable()
  48. {
  49. container.Add<ITestService, TestServiceA>();
  50. container.Add<ITestService, TestServiceB>();
  51. container.Add<IAsksForEnumerable, AsksForEnumerable>();
  52. var asks = container.Get<IAsksForEnumerable>();
  53. asks.ShouldNotBeNull();
  54. asks.SecondService.ShouldNotBeNull();
  55. asks.SecondService.ShouldBeInstanceOf<TestServiceB>();
  56. }
  57. }
  58. public class WhenGetAllIsCalledOnComponentContainer : ComponentContainerContext
  59. {
  60. [Fact]
  61. public void ReturnsSeriesWithSingleItem()
  62. {
  63. container.Add<ITestService, TestServiceA>();
  64. var services = container.GetAll<ITestService>().ToList();
  65. services.ShouldNotBeNull();
  66. services.Count.ShouldBe(1);
  67. services[0].ShouldBeInstanceOf<TestServiceA>();
  68. }
  69. [Fact]
  70. public void ReturnsInstanceOfEachRegisteredImplementation()
  71. {
  72. container.Add<ITestService, TestServiceA>();
  73. container.Add<ITestService, TestServiceB>();
  74. var services = container.GetAll<ITestService>().ToList();
  75. services.ShouldNotBeNull();
  76. services.Count.ShouldBe(2);
  77. services[0].ShouldBeInstanceOf<TestServiceA>();
  78. services[1].ShouldBeInstanceOf<TestServiceB>();
  79. }
  80. [Fact]
  81. public void ReturnsSameInstanceForTwoCallsForSameService()
  82. {
  83. container.Add<ITestService, TestServiceA>();
  84. var service1 = container.Get<ITestService>();
  85. var service2 = container.Get<ITestService>();
  86. service1.ShouldNotBeNull();
  87. service2.ShouldNotBeNull();
  88. service1.ShouldBeSameAs(service2);
  89. }
  90. }
  91. public class WhenRemoveAllIsCalled : ComponentContainerContext
  92. {
  93. [Fact]
  94. public void RemovesAllMappings()
  95. {
  96. container.Add<ITestService, TestServiceA>();
  97. var service1 = container.Get<ITestService>();
  98. service1.ShouldNotBeNull();
  99. container.RemoveAll<ITestService>();
  100. Assert.Throws<InvalidOperationException>(() => container.Get<ITestService>());
  101. }
  102. [Fact]
  103. public void DisposesOfAllInstances()
  104. {
  105. container.Add<ITestService, TestServiceA>();
  106. container.Add<ITestService, TestServiceB>();
  107. var services = container.GetAll<ITestService>().ToList();
  108. services.ShouldNotBeNull();
  109. services.Count.ShouldBe(2);
  110. container.RemoveAll<ITestService>();
  111. services[0].IsDisposed.ShouldBeTrue();
  112. services[1].IsDisposed.ShouldBeTrue();
  113. }
  114. }
  115. internal class AsksForEnumerable : NinjectComponent, IAsksForEnumerable
  116. {
  117. public ITestService SecondService { get; set; }
  118. public AsksForEnumerable(IEnumerable<ITestService> services)
  119. {
  120. SecondService = services.Skip(1).First();
  121. }
  122. }
  123. internal interface IAsksForEnumerable : INinjectComponent
  124. {
  125. ITestService SecondService { get; set; }
  126. }
  127. internal class TestServiceA : NinjectComponent, ITestService { }
  128. internal class TestServiceB : NinjectComponent, ITestService { }
  129. internal interface ITestService : INinjectComponent, INotifyWhenDisposed { }
  130. }