-
Notifications
You must be signed in to change notification settings - Fork 530
/
ConditionalBindingTests.cs
184 lines (156 loc) · 6.54 KB
/
ConditionalBindingTests.cs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
namespace Ninject.Tests.Integration
{
using System;
using System.Linq;
using FluentAssertions;
using Ninject.Tests.Fakes;
using Ninject.Tests.Integration.StandardKernelTests;
using Xunit;
public class ConditionalBindingTests: StandardKernelContext
{
[Fact]
public void GivenADefaultAndSingleSatisfiedConditional_ThenTheConditionalIsUsed()
{
kernel.Bind<IWeapon>().To<Sword>();
kernel.Bind<IWeapon>().To<Shuriken>().WhenInjectedInto<Samurai>();
kernel.Bind<Samurai>().ToSelf();
var warrior = kernel.Get<Samurai>();
warrior.Weapon.Should().BeOfType<Shuriken>();
}
[Fact]
public void GivenADefaultAndSingleUnatisfiedConditional_ThenTheDefaultIsUsed()
{
kernel.Bind<IWeapon>().To<Sword>();
kernel.Bind<IWeapon>().To<Shuriken>().WhenInjectedInto<Ninja>();
kernel.Bind<Samurai>().ToSelf();
var warrior = kernel.Get<Samurai>();
warrior.Weapon.Should().BeOfType<Sword>();
}
[Fact]
public void GivenADefaultAndAnUnSatisfiedConditional_ThenTheDefaultIsUsed()
{
kernel.Bind<IWeapon>().To<Sword>();
kernel.Bind<IWeapon>().To<Shuriken>().WhenInjectedInto<Ninja>();
kernel.Bind<Samurai>().ToSelf();
var warrior = kernel.Get<Samurai>();
warrior.Weapon.Should().BeOfType<Sword>();
}
[Fact]
public void GivenADefaultAndAnManySatisfiedConditionals_ThenAnExceptionIsThrown()
{
kernel.Bind<IWeapon>().To<Sword>();
kernel.Bind<IWeapon>().To<Sword>().WhenInjectedInto<Samurai>();
kernel.Bind<IWeapon>().To<Shuriken>().WhenInjectedInto<Samurai>();
kernel.Bind<Samurai>().ToSelf();
Assert.Throws<ActivationException>(() => kernel.Get<Samurai>());
}
[Fact]
public void GivenNoBinding_ThenASelfBindableTypeWillResolve()
{
var weapon = kernel.Get<Sword>();
weapon.Should().BeOfType<Sword>();
}
[Fact]
public void GivenBindingIsMadeAfterImplictBinding_ThenExplicitBindingWillResolve()
{
IWeapon weapon = kernel.Get<Sword>();
weapon.Should().BeOfType<Sword>();
kernel.Bind<Sword>().To<ShortSword>();
weapon = kernel.Get<Sword>();
weapon.Should().BeOfType<ShortSword>();
}
[Fact]
public void GivenBothImplicitAndExplicitConditionalBindings_ThenExplicitBindingWillResolve()
{
IWeapon weapon = kernel.Get<Sword>();
// make the binding conditional
kernel.GetBindings(typeof (Sword)).First().Condition = b => true;
weapon.Should().BeOfType<Sword>();
kernel.Bind<Sword>().To<ShortSword>().When(_ => true);
weapon = kernel.Get<Sword>();
weapon.Should().BeOfType<ShortSword>();
}
[Fact]
public void GivenADefaultAndAConditionalImplicitBinding_ThenConditionalBindingWillResolve()
{
IWeapon weapon = kernel.Get<Sword>();
// make the binding conditional
kernel.GetBindings(typeof (Sword)).First().Condition = b => true;
weapon.Should().BeOfType<Sword>();
kernel.Bind<Sword>().To<ShortSword>();
weapon = kernel.Get<Sword>();
weapon.Should().BeOfType<Sword>();
}
[Fact]
public void GivenADefaultAndAConditionalBinding_AllBindingsWillResolve()
{
var shortSword = new ShortSword();
var shuriken = new Shuriken();
kernel.Bind<IWeapon>().ToConstant(shortSword);
kernel.Bind<IWeapon>().ToConstant(shuriken).When(_ => true);
var result = kernel.GetAll<IWeapon>();
result.Should().Contain(shortSword);
result.Should().Contain(shuriken);
}
[Fact]
public void GivenAMixtureOfBindings_OnlyNonImplicitBindingsWillResolve()
{
var shortSword = new ShortSword();
var sword = new Sword();
var shuriken = new Shuriken();
kernel.Bind<IWeapon>().ToConstant(shortSword);
kernel.Bind<IWeapon>().ToConstant(sword);
kernel.Bind<IWeapon>().ToConstant(shuriken).Binding.IsImplicit = true;
var result = kernel.GetAll<IWeapon>();
result.Should().Contain(shortSword);
result.Should().Contain(sword);
result.Should().NotContain(shuriken);
}
[Fact]
public void GivenOnlyImplicitBindings_AllBindingsWillResolve()
{
var shortSword = new ShortSword();
var shuriken = new Shuriken();
kernel.Bind<IWeapon>().ToConstant(shortSword).Binding.IsImplicit = true;
kernel.Bind<IWeapon>().ToConstant(shuriken).Binding.IsImplicit = true;
var result = kernel.GetAll<IWeapon>();
result.Should().Contain(shortSword);
result.Should().Contain(shuriken);
}
[Fact]
public void WhenInjectedIntoAppliesToBaseTypes()
{
kernel.Bind<IWarrior>().To<Samurai>();
kernel.Bind<IWeapon>().To<Sword>().WhenInjectedInto<IWarrior>();
var warrior = kernel.Get<IWarrior>();
warrior.Weapon.Should().BeOfType<Sword>();
}
[Fact]
public void WhenInjectedExactlyIntoAppliesNotToBaseTypes()
{
kernel.Bind<IWarrior>().To<Samurai>();
kernel.Bind<IWeapon>().To<Sword>().WhenInjectedExactlyInto<IWarrior>();
Action getWarrior = () => kernel.Get<IWarrior>();
getWarrior.ShouldThrow<ActivationException>();
}
[Fact]
public void WhenInjectedExactlyIntoAppliesToServiceType()
{
kernel.Bind<IWarrior>().To<Samurai>();
kernel.Bind<IWeapon>().To<Sword>().WhenInjectedExactlyInto<Samurai>();
var warrior = kernel.Get<IWarrior>();
warrior.Weapon.Should().BeOfType<Sword>();
}
[Fact]
public void WhenAnyAnchestorNamedAppliesToGrandParentAndParent()
{
const string Name = "SomeName";
kernel.Bind<Barracks>().ToSelf().Named(Name);
kernel.Bind<IWarrior>().To<Samurai>();
kernel.Bind<IWeapon>().To<Sword>().WhenAnyAnchestorNamed(Name);
var barack = kernel.Get<Barracks>();
barack.Weapon.Should().BeOfType<Sword>();
barack.Warrior.Weapon.Should().BeOfType<Sword>();
}
}
}