I'm writing an Akka.NET Testkit implementation that uses FluentAssertions under the hood, but can't figure out how to write the 'last' Assertion: Equality using a custom Func equality comparer (while getting a nice Error message, of course).
public void AssertEqual<T>(T expected, T actual,
Func<T, T, bool> comparer,
string format = "", params object[] args)
{
// This works, but does not give a good message:
comparer(expected, actual).Should().BeTrue(format, args);
// But this doesn't work at all:
// actual.Should().BeEquivalentTo(expected, options =>
options.Using<T>( x => comparer(x.Subject, expected).Should().BeTrue())
.WhenTypeIs<T>(),
format, args);
}
I'm pretty sure there must be some fancy way in FA to do this, but I can't find it.
Why don't you replace the call to BeEquivalentTo
with a custom assertion built on top of Execute.Assertion
as documented in the extensibility section?
Or just use BeEquivalentTo
directly using its options? You can even wrap it in your own code like this:
public static void AssertEqual<T>(T actual, T expected,
Func<EquivalencyAssertionOptions<T>, EquivalencyAssertionOptions<T>> config)
{
Func<EquivalencyAssertionOptions<TEvent>, EquivalencyAssertionOptions<TEvent>> effectiveConfig = opt =>
{
// customize the assertion in addition to the caller's customization
return config(opt);
};
actual.Should().BeEquivalentTo(expected, effectiveConfig);
}