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

Language C# Lines 105
MD5 Hash 53225f2ff12a9f09c7d88fa006e8b5a6
Repository https://github.com/developingchris/ninject.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
using System;
using System.Reflection;
using Moq;
using Ninject.Activation;
using Ninject.Activation.Strategies;
using Ninject.Injection;
using Ninject.Parameters;
using Ninject.Planning;
using Ninject.Planning.Directives;
using Ninject.Planning.Targets;
using Xunit;
using Xunit.Should;

namespace Ninject.Tests.Unit.PropertyInjectionStrategyTests
{
	public class PropertyInjectionDirectiveContext
	{
		protected readonly PropertyInjectionStrategy strategy;

		public PropertyInjectionDirectiveContext()
		{
			strategy = new PropertyInjectionStrategy(null) {Settings = new NinjectSettings()};
		}
	}

	public class WhenActivateIsCalled : PropertyInjectionDirectiveContext
	{
		protected Dummy instance = new Dummy();
		protected PropertyInfo property1 = typeof(Dummy).GetProperty("Foo");
		protected PropertyInfo property2 = typeof(Dummy).GetProperty("Bar");
		protected InstanceReference reference;
		protected Mock<IContext> contextMock;
		protected Mock<IPlan> planMock;
		protected FakePropertyInjectionDirective[] directives;
		protected PropertyInjector injector1;
		protected PropertyInjector injector2;
		protected bool injector1WasCalled;
		protected bool injector2WasCalled;

		public WhenActivateIsCalled()
		{
			contextMock = new Mock<IContext>();
			planMock = new Mock<IPlan>();
			injector1 = (x, y) => { injector1WasCalled = true; };
			injector2 = (x, y) => { injector2WasCalled = true; };

			directives = new[]
			{
				new FakePropertyInjectionDirective(property1, injector1),
				new FakePropertyInjectionDirective(property2, injector2)
			};

			contextMock.SetupGet(x => x.Plan).Returns(planMock.Object);
			contextMock.SetupGet(x => x.Parameters).Returns(new IParameter[0]);

			reference = new InstanceReference { Instance = instance };

			planMock.Setup(x => x.GetAll<PropertyInjectionDirective>()).Returns(directives);
		}

		[Fact]
		public void ReadsMethodInjectorsFromPlan()
		{
			strategy.Activate(contextMock.Object, reference);

			planMock.Verify(x => x.GetAll<PropertyInjectionDirective>());
		}

		[Fact]
		public void ResolvesValuesForEachTargetOfEachDirective()
		{
			strategy.Activate(contextMock.Object, reference);

			directives.Map(d => d.TargetMock.Verify(x => x.ResolveWithin(contextMock.Object)));
		}

		[Fact]
		public void InvokesInjectorsForEachDirective()
		{
			strategy.Activate(contextMock.Object, reference);
			injector1WasCalled.ShouldBeTrue();
			injector2WasCalled.ShouldBeTrue();
		}
	}

	public class FakePropertyInjectionDirective : PropertyInjectionDirective
	{
		public Mock<ITarget> TargetMock { get; private set; }

		public FakePropertyInjectionDirective(PropertyInfo property, PropertyInjector injector)
			: base(property, injector) { }

		protected override ITarget CreateTarget(PropertyInfo property)
		{
			TargetMock = new Mock<ITarget>();
			return TargetMock.Object;
		}
	}

	public class Dummy
	{
		public int Foo { get; set; }
		public string Bar { get; set; }
	}
}
Back to Top