Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 671
ilitirit Avatar asked Dec 06 '25 13:12

ilitirit


1 Answers

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");
    }
}
like image 197
Dennis Doomen Avatar answered Dec 08 '25 06:12

Dennis Doomen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!