Skip to content

Commit

Permalink
Fix unit tests to take into account we have a parameterless construct…
Browse files Browse the repository at this point in the history
…or on one of the handlers.
  • Loading branch information
carlsixsmith-moj committed Dec 2, 2024
1 parent 9cc92e2 commit c1ad9d0
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions test/ArchitectureTests/ApplicationTests/RequestHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Cfo.Cats.Application.Common.Interfaces;
using DocumentFormat.OpenXml.Office2010.ExcelAc;
using FluentAssertions;
using MediatR;
using NUnit.Framework;
Expand All @@ -18,7 +20,11 @@ public void HandlersDoNotReferToDbContextDirectly()
var handlerTypes = ApplicationAssembly.GetTypes()
.Where(t => t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IRequestHandler<,>)))
.ToList();


List<string> warnings = new List<string>();
List<string> failures = new List<string>();


foreach (var handlerType in handlerTypes)
{
// Get constructors
Expand All @@ -28,14 +34,32 @@ public void HandlersDoNotReferToDbContextDirectly()
// Get parameters of the constructor
var parameters = constructor.GetParameters();

if (parameters.Length == 0)
{
warnings.Add($"Type {handlerType.FullName} has a parameterless constructor. Check this is correct.");
break;
}

// Ensure none of the parameters is of type IApplicationDbContext
parameters.Should().NotContain(p => p.ParameterType == typeof(IApplicationDbContext),
$"{handlerType.Name} should not accept IApplicationDbContext as a constructor parameter.");

if(parameters.Any(p => p.ParameterType == typeof(IApplicationDbContext)))
{
failures.Add($"{handlerType.FullName} should not accept IApplicationDbContext as a constructor parameter.");
}


// Ensure there is a parameter of type IUnitOfWork
parameters.Should().Contain(p => p.ParameterType == typeof(IUnitOfWork),
$"{handlerType.Name} should have IUnitOfWork as a constructor parameter.");
if (parameters.Any(p => p.ParameterType == typeof(IUnitOfWork)) == false)
{
failures.Add($"{handlerType.FullName} should accept IUnitOfWork as a constructor parameter.");
}
}
}

failures.Should().BeEmpty(string.Join("\n", failures));

if (warnings.Any())
{
Assert.Warn(string.Join("\n", warnings));
}
}
}

0 comments on commit c1ad9d0

Please sign in to comment.