Skip to content

Treat null and empty as same "state" #1825

Answered by jnyrup
ITaluone asked this question in Q&A
Discussion options

You must be logged in to vote

Here's an example using the linked documentation, which considers null and empty byte[] to be equivalent.

var subject = new { Value = (byte)null };
var expected = new { Value = new byte[0] };

subject.Should().BeEquivalentTo(expected, opt => opt
    .Using<byte[]>(ctx =>
    {
        if (ctx.Subject is null or { Length: 0 } && ctx.Expectation is null or { Length: 0 })
        {
            return;
        }

        ctx.Subject.Should().BeEquivalentTo(ctx.Expectation);
    })
    .WhenTypeIs<byte[]>()
);

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by ITaluone
Comment options

You must be logged in to vote
1 reply
@rklec
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants