简体   繁体   中英

How to create custom FluentAssertion error messages?

I have a test that looks something like this:

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");
    }
}

Test failure results in this message:

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

However, I would like the message to say:

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

(I already have a method to prettify the typename - type.GetPrettyName )

Going by the documentation and source code, it looks like I need to find a way to modify the Subject/Identifier (not 100% sure) but I can't find a way to do that.

I also tried using a Lazy context function in the AssertionScope constructor but this results in a modified closure.

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");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM