我正在编写一个使用FluentAssertions的Akka.NET Testkit实现,但无法弄清楚如何编写'last'Assertion:Equality使用自定义Func相等比较器(同时获得一个不错的错误消息,当然)。
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);
}
我很确定FA必须有一些奇特的方式才能做到这一点,但我找不到它。
答案 0 :(得分:0)
为什么不按照extensibility section中记录的BeEquivalentTo
内置的自定义断言替换对Execute.Assertion
的调用?
或者直接使用BeEquivalentTo
使用其选项?您甚至可以将它包装在您自己的代码中:
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);
}