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

Language C# Lines 124
MD5 Hash 2492ed18027cf516159f64a1878df4c9
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
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Reflection;
using Ninject.Injection;
using Ninject.Tests.Fakes;
using Xunit;
using Xunit.Should;

namespace Ninject.Tests.Unit.DynamicMethodInjectorFactoryTests
{
	public class DynamicMethodInjectorFactoryContext
	{
		protected DynamicMethodInjectorFactory injectorFactory;

		public DynamicMethodInjectorFactoryContext()
		{
			injectorFactory = new DynamicMethodInjectorFactory();
			injectorFactory.Settings = new NinjectSettings();
		}
	}

	public class WhenConstructorInjectorIsInvoked : DynamicMethodInjectorFactoryContext
	{
		protected ConstructorInfo constructor;
		protected ConstructorInjector injector;

		public WhenConstructorInjectorIsInvoked()
		{
			constructor = typeof(Samurai).GetConstructor(new[] { typeof(IWeapon) });
			injector = injectorFactory.Create(constructor);
		}

		[Fact]
		public void CallsConstructor()
		{
			var sword = new Sword();

			var samurai = injector.Invoke(new[] { sword }) as Samurai;

			samurai.ShouldNotBeNull();
			samurai.Weapon.ShouldBeSameAs(sword);
		}

		[Fact]
		public void CallsConstructorWithNullArgumentIfOneIsSpecified()
		{
			var samurai = injector.Invoke(new[] { (IWeapon)null }) as Samurai;

			samurai.ShouldNotBeNull();
			samurai.Weapon.ShouldBeNull();
		}
	}

	public class WhenPropertyInjectorIsInvoked : DynamicMethodInjectorFactoryContext
	{
		protected PropertyInfo property;
		protected PropertyInjector injector;

		public WhenPropertyInjectorIsInvoked()
		{
			property = typeof(Samurai).GetProperty("Weapon");
			injector = injectorFactory.Create(property);
		}

		[Fact]
		public void SetsPropertyValue()
		{
			var samurai = new Samurai(null);
			var sword = new Sword();

			injector.Invoke(samurai, sword);

			samurai.Weapon.ShouldBeSameAs(sword);
		}

		[Fact]
		public void SetsPropertyValueToNullIfInvokedWithNullArgument()
		{
			var samurai = new Samurai(new Sword());
			injector.Invoke(samurai, null);
			samurai.Weapon.ShouldBeNull();
		}
	}

	public class WhenMethodInjectorIsInvokedOnVoidMethod : DynamicMethodInjectorFactoryContext
	{
		protected MethodInfo method;
		protected MethodInjector injector;

		public WhenMethodInjectorIsInvokedOnVoidMethod()
		{
			method = typeof(Samurai).GetMethod("SetName");
			injector = injectorFactory.Create(method);
		}

		[Fact]
		public void CallsMethod()
		{
			var samurai = new Samurai(new Sword());
			injector.Invoke(samurai, new[] { "Bob" });
			samurai.Name.ShouldBe("Bob");
		}
	}

	public class WhenMethodInjectorIsInvokedOnNonVoidMethod : DynamicMethodInjectorFactoryContext
	{
		protected MethodInfo method;
		protected MethodInjector injector;

		public WhenMethodInjectorIsInvokedOnNonVoidMethod()
		{
			method = typeof(Samurai).GetMethod("Attack");
			injector = injectorFactory.Create(method);
		}

		[Fact]
		public void CallsMethod()
		{
			var samurai = new Samurai(new Sword());
			injector.Invoke(samurai, new[] { "evildoer" });
			samurai.IsBattleHardened.ShouldBeTrue();
		}
	}
}
Back to Top