shouldly icon indicating copy to clipboard operation
shouldly copied to clipboard

Custom ShouldBeNull gives wrong error message

Open Alxandr opened this issue 10 months ago • 0 comments

I have a custom struct called FieldValue<T> in my code-base that can be either null, unset or T (kinda like Nullable<T> but with an extra state). To write assertions on these values I have a extension-class with methods like ShouldBeNull and ShouldBeUnset implemented as following:

[DebuggerStepThrough]
[ShouldlyMethods]
[EditorBrowsable(EditorBrowsableState.Never)]
public static class FieldValueShouldExtensions
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    public static void ShouldBeNull<T>(this FieldValue<T> actual, string? customMessage = null)
        where T : notnull
    {
        actual.AssertAwesomely(static v => v.IsNull, actual, FieldValue<T>.Null, customMessage);
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static void ShouldBeUnset<T>(this FieldValue<T> actual, string? customMessage = null)
        where T : notnull
    {
        actual.AssertAwesomely(static v => v.IsUnset, actual, FieldValue<T>.Unset, customMessage);
    }
}

The ShouldBeNull method shows the wrong error message however, because it get's picked up by ShouldlyMessageGenerator which writes but was{expectedValue}. So the actual value is not printed, the expected value is.

Alxandr avatar Feb 26 '25 13:02 Alxandr