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

Language C# Lines 112
MD5 Hash e9a546d2caf8b8531708bb905127ace0
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
106
107
108
109
110
111
using System;
using System.Linq;
using System.Reflection;
using Moq;
using Ninject.Activation;
using Ninject.Activation.Strategies;
using Ninject.Injection;
using Ninject.Planning;
using Ninject.Planning.Directives;
using Ninject.Planning.Targets;
using Ninject.Tests.Fakes;
using Xunit;
using Xunit.Should;

namespace Ninject.Tests.Unit.MethodInjectionStrategyTests
{
	public class MethodInjectionStrategyContext
	{
		protected readonly MethodInjectionStrategy strategy;

		public MethodInjectionStrategyContext()
		{
			strategy = new MethodInjectionStrategy();
		}
	}

	public class WhenActivateIsCalled : MethodInjectionStrategyContext
	{
		protected Dummy instance = new Dummy();
		protected InstanceReference reference;
		protected MethodInfo method1 = typeof(Dummy).GetMethod("Foo");
		protected MethodInfo method2 = typeof(Dummy).GetMethod("Bar");
		protected Mock<IContext> contextMock;
		protected Mock<IPlan> planMock;
		protected FakeMethodInjectionDirective[] directives;
		protected MethodInjector injector1;
		protected MethodInjector injector2;
		protected bool injector1WasCalled;
		protected bool injector2WasCalled;

		public WhenActivateIsCalled()
		{
			reference = new InstanceReference { Instance = instance };

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

			directives = new[]
			{
				new FakeMethodInjectionDirective(method1, injector1),
				new FakeMethodInjectionDirective(method2, injector2)
			};

			contextMock.SetupGet(x => x.Plan).Returns(planMock.Object);

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

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

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

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

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

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

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

	public class FakeMethodInjectionDirective : MethodInjectionDirective
	{
		public Mock<ITarget>[] TargetMocks { get; private set; }

		public FakeMethodInjectionDirective(MethodInfo method, MethodInjector injector)
			: base(method, injector) { }

		protected override ITarget[] CreateTargetsFromParameters(MethodInfo method)
		{
			TargetMocks = method.GetParameters().Select(p => new Mock<ITarget>()).ToArray();
			return TargetMocks.Select(m => m.Object).ToArray();
		}
	}

	public class Dummy
	{
		public void Foo(int a, string b) { }
		public void Bar(IWeapon weapon) { }
	}
}
Back to Top