PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Ninject.Tests/Integration/ConditionalBindingTests.cs

https://github.com/ALyman/ninject
C# | 93 lines | 81 code | 10 blank | 2 comment | 0 complexity | e66d3565783fd30d7f76c71f2edccc0f MD5 | raw file
  1. using Ninject.Tests.Fakes;
  2. using Ninject.Tests.Integration.StandardKernelTests;
  3. using Xunit;
  4. using System.Linq;
  5. namespace Ninject.Tests.Integration
  6. {
  7. public class ConditionalBindingTests: StandardKernelContext
  8. {
  9. [Fact]
  10. public void GivenADefaultAndSingleSatisfiedConditional_ThenTheConditionalIsUsed()
  11. {
  12. kernel.Bind<IWeapon>().To<Sword>();
  13. kernel.Bind<IWeapon>().To<Shuriken>().WhenInjectedInto<Samurai>();
  14. kernel.Bind<Samurai>().ToSelf();
  15. var warrior = kernel.Get<Samurai>();
  16. Assert.IsType<Shuriken>(warrior.Weapon);
  17. }
  18. [Fact]
  19. public void GivenADefaultAndSingleUnatisfiedConditional_ThenTheDefaultIsUsed()
  20. {
  21. kernel.Bind<IWeapon>().To<Sword>();
  22. kernel.Bind<IWeapon>().To<Shuriken>().WhenInjectedInto<Ninja>();
  23. kernel.Bind<Samurai>().ToSelf();
  24. var warrior = kernel.Get<Samurai>();
  25. Assert.IsType<Sword>(warrior.Weapon);
  26. }
  27. [Fact]
  28. public void GivenADefaultAndAnUnSatisfiedConditional_ThenTheDefaultIsUsed()
  29. {
  30. kernel.Bind<IWeapon>().To<Sword>();
  31. kernel.Bind<IWeapon>().To<Shuriken>().WhenInjectedInto<Ninja>();
  32. kernel.Bind<Samurai>().ToSelf();
  33. var warrior = kernel.Get<Samurai>();
  34. Assert.IsType<Sword>(warrior.Weapon);
  35. }
  36. [Fact]
  37. public void GivenADefaultAndAnManySatisfiedConditionals_ThenAnExceptionIsThrown()
  38. {
  39. kernel.Bind<IWeapon>().To<Sword>();
  40. kernel.Bind<IWeapon>().To<Sword>().WhenInjectedInto<Samurai>();
  41. kernel.Bind<IWeapon>().To<Shuriken>().WhenInjectedInto<Samurai>();
  42. kernel.Bind<Samurai>().ToSelf();
  43. Assert.Throws<ActivationException>(() => kernel.Get<Samurai>());
  44. }
  45. [Fact]
  46. public void GivenNoBinding_ThenASelfBindableTypeWillResolve()
  47. {
  48. var weapon = kernel.Get<Sword>();
  49. Assert.IsType<Sword>(weapon);
  50. }
  51. [Fact]
  52. public void GivenBindingIsMadeAfterImplictBinding_ThenExplicitBindingWillResolve()
  53. {
  54. IWeapon weapon = kernel.Get<Sword>();
  55. Assert.IsType<Sword>(weapon);
  56. kernel.Bind<Sword>().To<ShortSword>();
  57. weapon = kernel.Get<Sword>();
  58. Assert.IsType<ShortSword>(weapon);
  59. }
  60. [Fact]
  61. public void GivenBothImplicitAndExplicitConditionalBindings_ThenExplicitBindingWillResolve()
  62. {
  63. IWeapon weapon = kernel.Get<Sword>();
  64. // make the binding conditional
  65. kernel.GetBindings(typeof (Sword)).First().Condition = b => true;
  66. Assert.IsType<Sword>(weapon);
  67. kernel.Bind<Sword>().To<ShortSword>().When(_ => true);
  68. weapon = kernel.Get<Sword>();
  69. Assert.IsType<ShortSword>(weapon);
  70. }
  71. [Fact]
  72. public void GivenADefaultAndAConditionalImplicitBinding_ThenConditionalBindingWillResolve()
  73. {
  74. IWeapon weapon = kernel.Get<Sword>();
  75. // make the binding conditional
  76. kernel.GetBindings(typeof (Sword)).First().Condition = b => true;
  77. Assert.IsType<Sword>(weapon);
  78. kernel.Bind<Sword>().To<ShortSword>();
  79. weapon = kernel.Get<Sword>();
  80. Assert.IsType<Sword>(weapon);
  81. }
  82. }
  83. }