Skip to content

Improve failure message for StringAssertions.Be #2050

@jnyrup

Description

@jnyrup

Discussed in #2048 and #1941

Originally posted by thomOrbelius November 30, 2022
Hi.

In my department we have been starting to use FluentAssertions. One of the selling point I had was the improved failure messages. A colleague of mine pointed out that the Xunit failure messages for strings is much more helpful (see example below) than the FluentAssertion equivalent. The example shows a case where we use a string with a newline separator.

Do any one else think that a message more similar to the one Xunit has is more helpful than the current? If so, should we update the message used?

using FluentAssertions;
using Xunit;

namespace Sectra.Courier.RisAdapter.Tests.Integrations.SectraRis;

public class FluentComparison {
    private const string string1 = "String with \rnewline";
    private const string string2 = "String with \ra newline";

    [Fact]
    public void FluentAssert() {
        // Fails with:
        // newline" has a length of 20, differs near "new" (index 13).
        string1.Should().Be(string2);
    }

    [Fact]
    public void XUnitAssert() {
        // Fails with:
        // Assert.Equal() Failure
        //                         ↓ (pos 13)
        // Expected: String with \rnewline
        // Actual:   String with \ra newline
        //                         ↑ (pos 13)
        Assert.Equal(string1, string2);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions