This is a simple etension method for Fluent Assertions called ShouldAll that can be run on a collection and you can pass in your predicate of your choice and see the output.
Consider this unit test:
var oneyearPeriodComparions = new []
{
new ReferencePeriodComparisonResult { ReferencePeriod = reportPeriod2017 , CalculatedPeriod = calculatedReportPeriod2017.First() },
new ReferencePeriodComparisonResult { ReferencePeriod = reportPeriod2017 , CalculatedPeriod = calculatedReportPeriod2017.Last()},
new ReferencePeriodComparisonResult { ReferencePeriod = reportPeriod2018 , CalculatedPeriod = calculatedReportPeriod2018.First()},
new ReferencePeriodComparisonResult { ReferencePeriod = reportPeriod2018 , CalculatedPeriod = calculatedReportPeriod2018.Last() },
new ReferencePeriodComparisonResult { ReferencePeriod = reportPeriod2019 , CalculatedPeriod = calculatedReportPeriod2019.First() },
new ReferencePeriodComparisonResult { ReferencePeriod = reportPeriod2019 , CalculatedPeriod = calculatedReportPeriod2019.Last() }
};
oneyearPeriodComparions.ShouldAll(comparison => comparison.CalculatedPeriod.ReportPeriodStartDateAndEndDateIsEqualTo(comparison.ReferencePeriod), outputPassingTests:true);
This uses this extension test for Fluent Assertions:
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace SomeAcme.SomeLib
{
public static class FluentAssertionsExtensions
{
/// <summary>
/// Inspects that all tests are passing for given collection and given predicate
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="instances"></param>
/// <param name="predicate"></param>
/// <param name="outputFailingTests"></param>
public static void ShouldAll<T>(this IEnumerable<T> instances, Expression<Func<T, bool>> predicate, bool outputFailingTests = true, bool outputPassingTests = false)
{
foreach (var instance in instances)
{
var isTestPassing = predicate.Compile().Invoke(instance);
if (!isTestPassing && outputFailingTests || outputPassingTests)
Console.WriteLine($@"Test Running against object: {instance} Test Pass?:{isTestPassing}");
isTestPassing.Should().Be(true);
}
}
}
}