ninject /src/Ninject.Tests/Integration/ConstructorSelectionTests.cs

Language C# Lines 90
MD5 Hash c8bd59e03b20b9ddd80dc330dae49b06
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
using Ninject.Parameters;
using Ninject.Tests.Fakes;
using Xunit;

namespace Ninject.Tests.Integration
{
	public class ConstructorSelectionTests
	{
		[Fact]
		public void DefaultCtorIsUsedWhenNoBindingAreAvailable()
		{
			using ( IKernel kernel = new StandardKernel() )
			{
				kernel.Bind<Barracks>().ToSelf();

				var barracks = kernel.Get<Barracks>();
				Assert.NotNull( barracks );
				Assert.Null( barracks.Warrior );
				Assert.Null( barracks.Weapon );
			}
		}

		[Fact]
		public void CtorIsUsedWhenParameterIsSupplied()
		{
			using(IKernel kernel = new StandardKernel())
			{
				kernel.Bind<Barracks>().ToSelf();
				var constructorArgument = new ConstructorArgument("warrior", new Samurai(new Sword()));
				var barracks = kernel.Get<Barracks>(constructorArgument);
				
				Assert.NotNull(barracks);
				Assert.NotNull(barracks.Warrior);
				Assert.NotNull(barracks.Warrior.Weapon);
				Assert.Null(barracks.Weapon);
			}
		}

		[Fact]
		public void FirstAvailableWithBindingAvailableIsUsed()
		{
			using ( IKernel kernel = new StandardKernel() )
			{
				kernel.Bind<Barracks>().ToSelf();
				kernel.Bind<IWeapon>().To<Sword>();

				var barracks = kernel.Get<Barracks>();
				Assert.NotNull( barracks );
				Assert.Null( barracks.Warrior );
				Assert.NotNull( barracks.Weapon );
			}
		}

		[Fact]
		public void CtorWithMostDependenciesIsUsedWhenBindingsAreAvailable()
		{
			using ( IKernel kernel = new StandardKernel() )
			{
				kernel.Bind<Barracks>().ToSelf();
				kernel.Bind<IWeapon>().To<Sword>();
				kernel.Bind<IWarrior>().To<Samurai>();

				var barracks = kernel.Get<Barracks>();
				Assert.NotNull( barracks );
				Assert.NotNull( barracks.Warrior );
				Assert.NotNull( barracks.Warrior.Weapon );
				Assert.NotNull( barracks.Weapon );
			}
		}

		[Fact]
		public void CreationWillFailIfAllDepenciesAreMissingAndInjectAttributeIsApplied()
		{
			using ( IKernel kernel = new StandardKernel() )
			{
				kernel.Bind<NinjaBarracks>().ToSelf();

				Assert.Throws<ActivationException>( () => kernel.Get<NinjaBarracks>() );

				kernel.Bind<IWeapon>().To<Sword>();
				Assert.Throws<ActivationException>( () => kernel.Get<NinjaBarracks>() );
				kernel.Unbind<IWeapon>();

				kernel.Bind<IWarrior>().To<Samurai>();
				Assert.Throws<ActivationException>( () => kernel.Get<NinjaBarracks>() );
				kernel.Unbind<IWarrior>();
			}
		}
	}
}
Back to Top