PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Ninject.Tests/Unit/PropertyInjectionStrategyTests.cs

https://github.com/developingchris/ninject
C# | 105 lines | 88 code | 17 blank | 0 comment | 0 complexity | e530a425642c93c2b9db3e7711f88cda MD5 | raw file
  1. using System;
  2. using System.Reflection;
  3. using Moq;
  4. using Ninject.Activation;
  5. using Ninject.Activation.Strategies;
  6. using Ninject.Injection;
  7. using Ninject.Parameters;
  8. using Ninject.Planning;
  9. using Ninject.Planning.Directives;
  10. using Ninject.Planning.Targets;
  11. using Xunit;
  12. using Xunit.Should;
  13. namespace Ninject.Tests.Unit.PropertyInjectionStrategyTests
  14. {
  15. public class PropertyInjectionDirectiveContext
  16. {
  17. protected readonly PropertyInjectionStrategy strategy;
  18. public PropertyInjectionDirectiveContext()
  19. {
  20. strategy = new PropertyInjectionStrategy(null) {Settings = new NinjectSettings()};
  21. }
  22. }
  23. public class WhenActivateIsCalled : PropertyInjectionDirectiveContext
  24. {
  25. protected Dummy instance = new Dummy();
  26. protected PropertyInfo property1 = typeof(Dummy).GetProperty("Foo");
  27. protected PropertyInfo property2 = typeof(Dummy).GetProperty("Bar");
  28. protected InstanceReference reference;
  29. protected Mock<IContext> contextMock;
  30. protected Mock<IPlan> planMock;
  31. protected FakePropertyInjectionDirective[] directives;
  32. protected PropertyInjector injector1;
  33. protected PropertyInjector injector2;
  34. protected bool injector1WasCalled;
  35. protected bool injector2WasCalled;
  36. public WhenActivateIsCalled()
  37. {
  38. contextMock = new Mock<IContext>();
  39. planMock = new Mock<IPlan>();
  40. injector1 = (x, y) => { injector1WasCalled = true; };
  41. injector2 = (x, y) => { injector2WasCalled = true; };
  42. directives = new[]
  43. {
  44. new FakePropertyInjectionDirective(property1, injector1),
  45. new FakePropertyInjectionDirective(property2, injector2)
  46. };
  47. contextMock.SetupGet(x => x.Plan).Returns(planMock.Object);
  48. contextMock.SetupGet(x => x.Parameters).Returns(new IParameter[0]);
  49. reference = new InstanceReference { Instance = instance };
  50. planMock.Setup(x => x.GetAll<PropertyInjectionDirective>()).Returns(directives);
  51. }
  52. [Fact]
  53. public void ReadsMethodInjectorsFromPlan()
  54. {
  55. strategy.Activate(contextMock.Object, reference);
  56. planMock.Verify(x => x.GetAll<PropertyInjectionDirective>());
  57. }
  58. [Fact]
  59. public void ResolvesValuesForEachTargetOfEachDirective()
  60. {
  61. strategy.Activate(contextMock.Object, reference);
  62. directives.Map(d => d.TargetMock.Verify(x => x.ResolveWithin(contextMock.Object)));
  63. }
  64. [Fact]
  65. public void InvokesInjectorsForEachDirective()
  66. {
  67. strategy.Activate(contextMock.Object, reference);
  68. injector1WasCalled.ShouldBeTrue();
  69. injector2WasCalled.ShouldBeTrue();
  70. }
  71. }
  72. public class FakePropertyInjectionDirective : PropertyInjectionDirective
  73. {
  74. public Mock<ITarget> TargetMock { get; private set; }
  75. public FakePropertyInjectionDirective(PropertyInfo property, PropertyInjector injector)
  76. : base(property, injector) { }
  77. protected override ITarget CreateTarget(PropertyInfo property)
  78. {
  79. TargetMock = new Mock<ITarget>();
  80. return TargetMock.Object;
  81. }
  82. }
  83. public class Dummy
  84. {
  85. public int Foo { get; set; }
  86. public string Bar { get; set; }
  87. }
  88. }