如何创建自定义 FluentAssertion 错误消息?

How to create custom FluentAssertion error messages?

我有一个看起来像这样的测试:

using (new AssertionScope())
{
    foreach (var parameterType in parameterTypes)
    {
        var fooType = typeof(IFoo<>);
        var genericType = providerType.MakeGenericType(parameterType);

        serviceProvider
            .GetService(fooType)
            .Should()
            .NotBeNull($"all Foo types should be registered");
    }
}

此消息中的测试失败结果:

Expected serviceProvider.GetService(genericProviderType) not to be <null> because all Foo types should be registered.

不过,我想留言说:

Expected IFoo<MyParameter> not to be <null> because all Foo types should be registered.

(我已经有了美化类型名的方法 - type.GetPrettyName

根据文档和源代码,我似乎需要找到一种方法来修改 Subject/Identifier(不是 100% 确定),但我找不到这样做的方法。

我还尝试在 AssertionScope 构造函数中使用惰性上下文函数,但这会导致修改后的闭包。

using (new AssertionScope())
{
    foreach (var parameterType in parameterTypes)
    {
        var fooType = typeof(IFoo<>);
        var genericType = providerType.MakeGenericType(parameterType);

        using _ = new AssertionScope(fooType.GetPrettyName());
       
        serviceProvider
            .GetService(fooType)
            .Should()
            .NotBeNull($"all Foo types should be registered");
    }
}