我有一个看起来像这样的测试:
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
)
根据文档和源代码,看起来我需要找到一种方法来修改主题/标识符(不是 100% 确定),但我找不到方法来做到这一点。
我还尝试在 AssertionScope 构造函数中使用 Lazy context 函数,但这会导致闭包被修改。
最佳答案
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");
}
}
关于c# - 如何创建自定义 FluentAssertion 错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68983596/