PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Ninject.Tests/Unit/DynamicMethodInjectorFactoryTests.cs

https://github.com/ALyman/ninject
C# | 125 lines | 104 code | 21 blank | 0 comment | 0 complexity | 02959f35d75687e25870feb128df8f44 MD5 | raw file
  1. #if !NO_LCG
  2. using System;
  3. using System.Reflection;
  4. using Ninject.Injection;
  5. using Ninject.Tests.Fakes;
  6. using Xunit;
  7. using Xunit.Should;
  8. namespace Ninject.Tests.Unit.DynamicMethodInjectorFactoryTests
  9. {
  10. public class DynamicMethodInjectorFactoryContext
  11. {
  12. protected DynamicMethodInjectorFactory injectorFactory;
  13. public DynamicMethodInjectorFactoryContext()
  14. {
  15. injectorFactory = new DynamicMethodInjectorFactory();
  16. injectorFactory.Settings = new NinjectSettings();
  17. }
  18. }
  19. public class WhenConstructorInjectorIsInvoked : DynamicMethodInjectorFactoryContext
  20. {
  21. protected ConstructorInfo constructor;
  22. protected ConstructorInjector injector;
  23. public WhenConstructorInjectorIsInvoked()
  24. {
  25. constructor = typeof(Samurai).GetConstructor(new[] { typeof(IWeapon) });
  26. injector = injectorFactory.Create(constructor);
  27. }
  28. [Fact]
  29. public void CallsConstructor()
  30. {
  31. var sword = new Sword();
  32. var samurai = injector.Invoke(new[] { sword }) as Samurai;
  33. samurai.ShouldNotBeNull();
  34. samurai.Weapon.ShouldBeSameAs(sword);
  35. }
  36. [Fact]
  37. public void CallsConstructorWithNullArgumentIfOneIsSpecified()
  38. {
  39. var samurai = injector.Invoke(new[] { (IWeapon)null }) as Samurai;
  40. samurai.ShouldNotBeNull();
  41. samurai.Weapon.ShouldBeNull();
  42. }
  43. }
  44. public class WhenPropertyInjectorIsInvoked : DynamicMethodInjectorFactoryContext
  45. {
  46. protected PropertyInfo property;
  47. protected PropertyInjector injector;
  48. public WhenPropertyInjectorIsInvoked()
  49. {
  50. property = typeof(Samurai).GetProperty("Weapon");
  51. injector = injectorFactory.Create(property);
  52. }
  53. [Fact]
  54. public void SetsPropertyValue()
  55. {
  56. var samurai = new Samurai(null);
  57. var sword = new Sword();
  58. injector.Invoke(samurai, sword);
  59. samurai.Weapon.ShouldBeSameAs(sword);
  60. }
  61. [Fact]
  62. public void SetsPropertyValueToNullIfInvokedWithNullArgument()
  63. {
  64. var samurai = new Samurai(new Sword());
  65. injector.Invoke(samurai, null);
  66. samurai.Weapon.ShouldBeNull();
  67. }
  68. }
  69. public class WhenMethodInjectorIsInvokedOnVoidMethod : DynamicMethodInjectorFactoryContext
  70. {
  71. protected MethodInfo method;
  72. protected MethodInjector injector;
  73. public WhenMethodInjectorIsInvokedOnVoidMethod()
  74. {
  75. method = typeof(Samurai).GetMethod("SetName");
  76. injector = injectorFactory.Create(method);
  77. }
  78. [Fact]
  79. public void CallsMethod()
  80. {
  81. var samurai = new Samurai(new Sword());
  82. injector.Invoke(samurai, new[] { "Bob" });
  83. samurai.Name.ShouldBe("Bob");
  84. }
  85. }
  86. public class WhenMethodInjectorIsInvokedOnNonVoidMethod : DynamicMethodInjectorFactoryContext
  87. {
  88. protected MethodInfo method;
  89. protected MethodInjector injector;
  90. public WhenMethodInjectorIsInvokedOnNonVoidMethod()
  91. {
  92. method = typeof(Samurai).GetMethod("Attack");
  93. injector = injectorFactory.Create(method);
  94. }
  95. [Fact]
  96. public void CallsMethod()
  97. {
  98. var samurai = new Samurai(new Sword());
  99. injector.Invoke(samurai, new[] { "evildoer" });
  100. samurai.IsBattleHardened.ShouldBeTrue();
  101. }
  102. }
  103. }
  104. #endif //!NO_LCG