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

Language C# Lines 351
MD5 Hash b85301c5e5271b3a631df1f2a3b59055
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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using System;
using System.Collections.Generic;
using System.Linq;
using Ninject.Tests.Fakes;
using Xunit;
using Xunit.Should;

namespace Ninject.Tests.Integration.StandardKernelTests
{
	public class StandardKernelContext
	{
		protected readonly StandardKernel kernel;

		public StandardKernelContext()
		{
			kernel = new StandardKernel();
		}
	}

	public class WhenGetIsCalledForInterfaceBoundService : StandardKernelContext
	{
		[Fact]
		public void SingleInstanceIsReturnedWhenOneBindingIsRegistered()
		{
			kernel.Bind<IWeapon>().To<Sword>();

			var weapon = kernel.Get<IWeapon>();

			weapon.ShouldNotBeNull();
			weapon.ShouldBeInstanceOf<Sword>();
		}

		[Fact]
		public void FirstInstanceIsReturnedWhenMultipleBindingsAreRegistered()
		{
			kernel.Bind<IWeapon>().To<Sword>();
			kernel.Bind<IWeapon>().To<Shuriken>();

			var weapon = kernel.Get<IWeapon>();

			weapon.ShouldNotBeNull();
			weapon.ShouldBeInstanceOf<Sword>();
		}

		[Fact]
		public void DependenciesAreInjectedViaConstructor()
		{
			kernel.Bind<IWeapon>().To<Sword>();
			kernel.Bind<IWarrior>().To<Samurai>();

			var warrior = kernel.Get<IWarrior>();

			warrior.ShouldNotBeNull();
			warrior.ShouldBeInstanceOf<Samurai>();
			warrior.Weapon.ShouldNotBeNull();
			warrior.Weapon.ShouldBeInstanceOf<Sword>();
		}
	}

	public class WhenGetIsCalledForSelfBoundService : StandardKernelContext
	{
		[Fact]
		public void SingleInstanceIsReturnedWhenOneBindingIsRegistered()
		{
			kernel.Bind<Sword>().ToSelf();

			var weapon = kernel.Get<Sword>();

			weapon.ShouldNotBeNull();
			weapon.ShouldBeInstanceOf<Sword>();
		}

		[Fact]
		public void DependenciesAreInjectedViaConstructor()
		{
			kernel.Bind<IWeapon>().To<Sword>();
			kernel.Bind<Samurai>().ToSelf();

			var samurai = kernel.Get<Samurai>();

			samurai.ShouldNotBeNull();
			samurai.Weapon.ShouldNotBeNull();
			samurai.Weapon.ShouldBeInstanceOf<Sword>();
		}
	}

	public class WhenGetIsCalledForUnboundService : StandardKernelContext
	{
		[Fact]
		public void ImplicitSelfBindingIsRegisteredAndActivated()
		{
			var weapon = kernel.Get<Sword>();

			weapon.ShouldNotBeNull();
			weapon.ShouldBeInstanceOf<Sword>();
		}

		[Fact]
		public void ImplicitSelfBindingForGenericTypeIsRegisteredAndActivated()
		{
			var service = kernel.Get<GenericService<int>>();

			service.ShouldNotBeNull();
			service.ShouldBeInstanceOf<GenericService<int>>();
		}

		[Fact]
		public void ThrowsExceptionIfAnUnboundInterfaceIsRequested()
		{
			Assert.Throws<ActivationException>(() => kernel.Get<IWeapon>());
		}

		[Fact]
		public void ThrowsExceptionIfAnUnboundAbstractClassIsRequested()
		{
			Assert.Throws<ActivationException>(() => kernel.Get<AbstractWeapon>());
		}

		[Fact]
		public void ThrowsExceptionIfAnUnboundValueTypeIsRequested()
		{
			Assert.Throws<ActivationException>(() => kernel.Get<int>());
		}

		[Fact]
		public void ThrowsExceptionIfAStringIsRequestedWithNoBinding()
		{
			Assert.Throws<ActivationException>(() => kernel.Get<string>());
		}

		[Fact]
		public void ThrowsExceptionIfAnOpenGenericTypeIsRequested()
		{
			Assert.Throws<ActivationException>(() => kernel.Get(typeof(IGeneric<>)));
		}
	}

	public class WhenGetIsCalledForGenericServiceRegisteredViaOpenGenericType : StandardKernelContext
	{
		[Fact]
		public void GenericParametersAreInferred()
		{
			kernel.Bind(typeof(IGeneric<>)).To(typeof(GenericService<>));

			var service = kernel.Get<IGeneric<int>>();

			service.ShouldNotBeNull();
			service.ShouldBeInstanceOf<GenericService<int>>();
		}
	}

	public class WhenTryGetIsCalledForInterfaceBoundService : StandardKernelContext
	{
		public void SingleInstanceIsReturnedWhenOneBindingIsRegistered()
		{
			kernel.Bind<IWeapon>().To<Sword>();

			var weapon = kernel.TryGet<IWeapon>();

			weapon.ShouldNotBeNull();
			weapon.ShouldBeInstanceOf<Sword>();
		}

		public void FirstInstanceIsReturnedWhenMultipleBindingsAreRegistered()
		{
			kernel.Bind<IWeapon>().To<Sword>();
			kernel.Bind<IWeapon>().To<Shuriken>();

			var weapon = kernel.TryGet<IWeapon>();

			weapon.ShouldNotBeNull();
			weapon.ShouldBeInstanceOf<Sword>();
		}
	}

	public class WhenTryGetIsCalledForUnboundService : StandardKernelContext
	{
		public void ImplicitSelfBindingIsRegisteredAndActivatedIfTypeIsSelfBindable()
		{
			var weapon = kernel.TryGet<Sword>();

			weapon.ShouldNotBeNull();
			weapon.ShouldBeInstanceOf<Sword>();
		}

		public void ReturnsNullIfTypeIsNotSelfBindable()
		{
			var weapon = kernel.TryGet<IWeapon>();
			weapon.ShouldBeNull();
		}
	}

	public class WhenGetAllIsCalledForInterfaceBoundService : StandardKernelContext
	{
		[Fact]
		public void ReturnsSeriesOfItemsInOrderTheyWereBound()
		{
			kernel.Bind<IWeapon>().To<Sword>();
			kernel.Bind<IWeapon>().To<Shuriken>();

			var weapons = kernel.GetAll<IWeapon>().ToArray();

			weapons.ShouldNotBeNull();
			weapons.Length.ShouldBe(2);
			weapons[0].ShouldBeInstanceOf<Sword>();
			weapons[1].ShouldBeInstanceOf<Shuriken>();
		}

		[Fact]
		public void DoesNotActivateItemsUntilTheEnumeratorRunsOverThem()
		{
			kernel.Bind<IInitializable>().To<InitializableA>();
			kernel.Bind<IInitializable>().To<InitializableB>();

			IEnumerable<IInitializable> instances = kernel.GetAll<IInitializable>();
			IEnumerator<IInitializable> enumerator = instances.GetEnumerator();

			InitializableA.Count.ShouldBe(0);
			enumerator.MoveNext();
			InitializableA.Count.ShouldBe(1);
			InitializableB.Count.ShouldBe(0);
			enumerator.MoveNext();
			InitializableA.Count.ShouldBe(1);
			InitializableB.Count.ShouldBe(1);
		}
	}

	public class WhenGetAllIsCalledForGenericServiceRegisteredViaOpenGenericType : StandardKernelContext
	{
		[Fact]
		public void GenericParametersAreInferred()
		{
			kernel.Bind(typeof(IGeneric<>)).To(typeof(GenericService<>));
			kernel.Bind(typeof(IGeneric<>)).To(typeof(GenericService2<>));

			var services = kernel.GetAll<IGeneric<int>>().ToArray();

			services.ShouldNotBeNull();
			services.Length.ShouldBe(2);
			services[0].ShouldBeInstanceOf<GenericService<int>>();
			services[1].ShouldBeInstanceOf<GenericService2<int>>();
		}
	}

	public class WhenGetAllIsCalledForUnboundService : StandardKernelContext
	{
		public void ImplicitSelfBindingIsRegisteredAndActivatedIfTypeIsSelfBindable()
		{
			var weapons = kernel.GetAll<Sword>().ToArray();

			weapons.ShouldNotBeNull();
			weapons.Length.ShouldBe(1);
			weapons[0].ShouldBeInstanceOf<Sword>();
		}

		public void ReturnsEmptyEnumerableIfTypeIsNotSelfBindable()
		{
			var weapons = kernel.GetAll<IWeapon>().ToArray();

			weapons.ShouldNotBeNull();
			weapons.Length.ShouldBe(0);
		}
	}

	public class WhenGetIsCalledWithConstraints : StandardKernelContext
	{
		[Fact]
		public void ReturnsServiceRegisteredViaBindingWithSpecifiedName()
		{
			kernel.Bind<IWeapon>().To<Shuriken>();
			kernel.Bind<IWeapon>().To<Sword>().Named("sword");

			var weapon = kernel.Get<IWeapon>("sword");

			weapon.ShouldNotBeNull();
			weapon.ShouldBeInstanceOf<Sword>();
		}

		[Fact]
		public void ReturnsServiceRegisteredViaBindingThatMatchesPredicate()
		{
			kernel.Bind<IWeapon>().To<Shuriken>().WithMetadata("type", "range");
			kernel.Bind<IWeapon>().To<Sword>().WithMetadata("type", "melee");

			var weapon = kernel.Get<IWeapon>(x => x.Get<string>("type") == "melee");

			weapon.ShouldNotBeNull();
			weapon.ShouldBeInstanceOf<Sword>();
		}
	}

	public class WhenUnbindIsCalled : StandardKernelContext
	{
		[Fact]
		public void RemovesAllBindingsForService()
		{
			kernel.Bind<IWeapon>().To<Shuriken>();
			kernel.Bind<IWeapon>().To<Sword>();

			var bindings = kernel.GetBindings(typeof(IWeapon)).ToArray();
			bindings.Length.ShouldBe(2);

			kernel.Unbind<IWeapon>();
			bindings = kernel.GetBindings(typeof(IWeapon)).ToArray();
			bindings.ShouldBeEmpty();
		}
	}

	public class WhenRebindIsCalled : StandardKernelContext
	{
		[Fact]
		public void RemovesAllBindingsForServiceAndReplacesWithSpecifiedBinding()
		{
			kernel.Bind<IWeapon>().To<Shuriken>();
			kernel.Bind<IWeapon>().To<Sword>();

			var bindings = kernel.GetBindings(typeof(IWeapon)).ToArray();
			bindings.Length.ShouldBe(2);

			kernel.Rebind<IWeapon>().To<Sword>();
			bindings = kernel.GetBindings(typeof(IWeapon)).ToArray();
			bindings.Length.ShouldBe(1);
		}
	}

	public class InitializableA : IInitializable
	{
		public static int Count = 0;

		public void Initialize()
		{
			Count++;
		}
	}

	public class InitializableB : IInitializable
	{
		public static int Count = 0;

		public void Initialize()
		{
			Count++;
		}
	}

	public interface IGeneric<T> { }
	public class GenericService<T> : IGeneric<T> { }
	public class GenericService2<T> : IGeneric<T> { }
	public interface IGenericWithConstraints<T> where T : class { }
	public class GenericServiceWithConstraints<T> : IGenericWithConstraints<T> where T : class { }
}
Back to Top