(#1313) Add new test to TypeRegistrarBaseTests

to assert the assumptions we're making in the code.
This commit is contained in:
Nils Andresen 2023-11-10 22:59:05 +01:00 committed by Patrik Svensson
parent 1fd028942f
commit ead7115cbe

View File

@ -23,13 +23,16 @@ public sealed class TypeRegistrarBaseTests
/// <exception cref="TestFailedException">This exception is raised, if a test fails.</exception>
public void RunAllTests()
{
var testCases = new Action<ITypeRegistrar>[]
var testCases = new[]
{
RegistrationsCanBeResolved,
InstanceRegistrationsCanBeResolved,
LazyRegistrationsCanBeResolved,
ResolvingNotRegisteredServiceReturnsNull,
ResolvingNullTypeReturnsNull,
ResolvingSingleInstanceOfMultipleRegistrationsResolvesTheFirstOne,
ResolvingAnEnumerableOfInstancesDoesNotReturnNull,
ResolvingAnEnumerableOfInstancesOfMultipleRegistrationsResolvesAllRegistrations,
};
foreach (var test in testCases)
@ -38,6 +41,80 @@ public sealed class TypeRegistrarBaseTests
}
}
private void ResolvingAnEnumerableOfInstancesDoesNotReturnNull(ITypeRegistrar registrar)
{
// Given
var resolver = registrar.Build();
// When
var actual = resolver.Resolve(typeof(IEnumerable<IMockService>)) as IEnumerable<IMockService>;
// Then
if (actual == null)
{
throw new TestFailedException(
"Expected an IEnumerable never to resolve to null.");
}
}
private void ResolvingAnEnumerableOfInstancesOfMultipleRegistrationsResolvesAllRegistrations(ITypeRegistrar registrar)
{
// Given
var theLastRegistration = new AnotherMockService("last");
registrar.RegisterLazy(typeof(IMockService), () => new AnotherMockService("first"));
registrar.Register(typeof(IMockService), typeof(MockService));
registrar.RegisterInstance(typeof(IMockService), theLastRegistration);
var resolver = registrar.Build();
// When
var actual = (resolver.Resolve(typeof(IEnumerable<IMockService>)) as IEnumerable<IMockService>)!.ToList();
// Then
if (actual.Count != 3)
{
throw new TestFailedException(
"Expected the resolver to resolve a list with exactly 3 elements.");
}
if (actual.Count(x => x.GetType() == typeof(AnotherMockService)) != 2)
{
throw new TestFailedException(
$"Expected the resolver to resolve a list with exactly 2 elements of type {nameof(AnotherMockService)}.");
}
if (actual.Count(x => x.GetType() == typeof(MockService)) != 1)
{
throw new TestFailedException(
$"Expected the resolver to resolve a list with exactly one element of type {nameof(MockService)}.");
}
if (!actual.Contains(theLastRegistration))
{
throw new TestFailedException(
"Expected the resolver to resolve the known instance that was registered.");
}
}
private void ResolvingSingleInstanceOfMultipleRegistrationsResolvesTheFirstOne(ITypeRegistrar registrar)
{
// Given
var theLastRegistration = new AnotherMockService("last");
registrar.RegisterLazy(typeof(IMockService), () => new AnotherMockService("first"));
registrar.Register(typeof(IMockService), typeof(MockService));
registrar.RegisterInstance(typeof(IMockService), theLastRegistration);
var resolver = registrar.Build();
// When
var actual = resolver.Resolve(typeof(IMockService));
// Then
if (!ReferenceEquals(actual, theLastRegistration))
{
throw new TestFailedException(
"Expected the resolver to resolve the first registered instance of multiple registrations.");
}
}
private static void ResolvingNullTypeReturnsNull(ITypeRegistrar registrar)
{
// Given no registration
@ -167,6 +244,11 @@ public sealed class TypeRegistrarBaseTests
{
}
private class AnotherMockService : IMockService
{
public AnotherMockService(string _){}
}
/// <summary>
/// Exception, to be raised when a test fails.
/// </summary>