PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Ninject.Tests/Integration/PropertyInjectionTests.cs

https://github.com/daniyord/ninject
C# | 124 lines | 112 code | 12 blank | 0 comment | 2 complexity | 785b9eda99834b20675f8051ec463d93 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. using Ninject.Infrastructure.Disposal;
  2. using Ninject.Parameters;
  3. using Ninject.Tests.Fakes;
  4. using Xunit;
  5. namespace Ninject.Tests.Integration
  6. {
  7. public class WithPropertyValueTests : PropertyInjectionTests
  8. {
  9. [Fact]
  10. public void PropertyValueIsAssignedWhenNoInjectAttributeIsSuppliedUsingCallback()
  11. {
  12. _kernel.Bind<IWarrior>().To<FootSoldier>()
  13. .WithPropertyValue("Weapon", context => context.Kernel.Get<IWeapon>());
  14. var warrior = _kernel.Get<IWarrior>();
  15. ValidateWarrior(warrior);
  16. }
  17. [Fact]
  18. public void PropertyValueIsAssignedWhenNoInjectAttributeUsingSuppliedValue()
  19. {
  20. _kernel.Bind<IWarrior>().To<FootSoldier>()
  21. .WithPropertyValue("Weapon", _kernel.Get<IWeapon>());
  22. var warrior = _kernel.Get<IWarrior>();
  23. ValidateWarrior(warrior);
  24. }
  25. [Fact]
  26. public void PropertyValuesOverrideDefaultBinding()
  27. {
  28. _kernel.Settings.InjectNonPublic = true;
  29. _kernel.Bind<IWarrior>().To<Ninja>()
  30. .WithPropertyValue("SecondaryWeapon", context => new Sword())
  31. .WithPropertyValue("SecretWeapon", context => new Sword());
  32. var warrior = _kernel.Get<IWarrior>();
  33. ValidateNinjaWarriorWithOverides(warrior);
  34. }
  35. }
  36. public class WithParameterTests : PropertyInjectionTests
  37. {
  38. [Fact]
  39. public void PropertyValueIsAssignedWhenNoInjectAttributeIsSuppliedUsingCallback()
  40. {
  41. _kernel.Bind<IWarrior>().To<FootSoldier>()
  42. .WithParameter(new PropertyValue("Weapon", context => context.Kernel.Get<IWeapon>()));
  43. var warrior = _kernel.Get<IWarrior>();
  44. ValidateWarrior(warrior);
  45. }
  46. [Fact]
  47. public void PropertyValueIsAssignedWhenNoInjectAttributeUsingSuppliedValue()
  48. {
  49. _kernel.Bind<IWarrior>().To<FootSoldier>()
  50. .WithParameter(new PropertyValue("Weapon", _kernel.Get<IWeapon>()));
  51. var warrior = _kernel.Get<IWarrior>();
  52. ValidateWarrior(warrior);
  53. }
  54. [Fact]
  55. public void PropertyValuesOverrideDefaultBinding()
  56. {
  57. _kernel.Settings.InjectNonPublic = true;
  58. _kernel.Bind<IWarrior>().To<Ninja>()
  59. .WithParameter(new PropertyValue("SecondaryWeapon", context => new Sword()))
  60. .WithParameter(new PropertyValue("SecretWeapon", context => new Sword()));
  61. var warrior = _kernel.Get<IWarrior>();
  62. ValidateNinjaWarriorWithOverides(warrior);
  63. }
  64. }
  65. public class WhenNoPropertyOverridesAreSupplied : PropertyInjectionTests
  66. {
  67. [Fact]
  68. public void DefaultBindingsAreUsed()
  69. {
  70. _kernel.Settings.InjectNonPublic = true;
  71. _kernel.Bind<IWarrior>().To<Ninja>();
  72. var warrior = _kernel.Get<IWarrior>();
  73. Assert.IsType<Ninja>(warrior);
  74. Assert.IsType<Shuriken>(warrior.Weapon);
  75. Ninja ninja = warrior as Ninja;
  76. Assert.IsType<Shuriken>(ninja.SecondaryWeapon);
  77. Assert.IsType<Shuriken>(ninja.SecretWeaponAccessor);
  78. }
  79. }
  80. public abstract class PropertyInjectionTests : DisposableObject
  81. {
  82. protected IKernel _kernel;
  83. public PropertyInjectionTests()
  84. {
  85. _kernel = new StandardKernel();
  86. _kernel.Bind<IWeapon>().To<Shuriken>();
  87. }
  88. protected void ValidateWarrior(IWarrior warrior)
  89. {
  90. Assert.IsType<FootSoldier>(warrior);
  91. Assert.NotNull(warrior.Weapon);
  92. Assert.IsType<Shuriken>(warrior.Weapon);
  93. }
  94. protected void ValidateNinjaWarriorWithOverides(IWarrior warrior)
  95. {
  96. Assert.IsType<Ninja>(warrior);
  97. Assert.IsType<Shuriken>(warrior.Weapon);
  98. Ninja ninja = warrior as Ninja;
  99. Assert.IsType<Sword>(ninja.SecondaryWeapon);
  100. Assert.IsType<Sword>(ninja.SecretWeaponAccessor);
  101. }
  102. public override void Dispose(bool disposing)
  103. {
  104. if (disposing && !IsDisposed)
  105. {
  106. _kernel.Dispose();
  107. _kernel = null;
  108. }
  109. base.Dispose(disposing);
  110. }
  111. }
  112. }