question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve failure message for `StringAssertions.Be`

See original GitHub issue

Discussed in https://github.com/fluentassertions/fluentassertions/discussions/2048 and https://github.com/fluentassertions/fluentassertions/discussions/1941

<div type='discussions-op-text'>

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);
    }
}
</div>

Issue Analytics

  • State:open
  • Created 10 months ago
  • Reactions:1
  • Comments:31 (30 by maintainers)

github_iconTop GitHub Comments

2reactions
jnyrupcommented, Dec 28, 2022

I think that the approach with identifying just the first mismatch is good enough.

I agree that we should probably pursue showing the first mismatch with pointers plus the N leading characters and M trailing characters.

2reactions
thomOrbeliuscommented, Dec 28, 2022

Mostly a shower thought, I’m not sure it applies well here.

Sequence Alignment is an area in computer science to find an aligment between two sequences, it could be applied to subject and expected.

E.g. one such alignment between "This is a test" and "This is another test" could be:

"This is a       test"
"This is another test"

Hirschberg’s algorithm can be used to compute a maximal global alignment between two sequences.

While I do think that the alignment approach is quite neat. But, if I just look at the result as you showed above, the risk it that I think that there is actually a lot if whitespaces in the first string but the lengths are correct, which could be confusing. The whitespace could of course be replaced by a placeholder symbol (· or *) or indicated on a separate “diff” line, but then the solution is approaching a diff tool again.

I think that the approach with identifying just the first mismatch is good enough. Or rather, I can’t think of a simple way to intuitively display both indices of mismatches combined with the alignment without it getting cluttered or confusing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tips - Fluent Assertions
The examples below show how you might improve your existing assertions to both get more readable assertions and much more informative failure messages....
Read more >
9 Fluent Assertions Tricks to Save Hours of Your Testing ...
Fluent assertions make your tests more readable and easier to maintain. In addition, they improve the overall quality of your tests by providing...
Read more >
c# - StringAssert.Contains("{", needle) throws System. ...
A quick solution was to use: StringAssert.Contains(my_text, my_needle, "", null);. Which works correctly! Explanation: Using the debugger I ...
Read more >
Advanced googletest Topics
Predicate Assertions for Better Error Messages ... As an example, let's improve the failure message in MutuallyPrime() , which ... More String Assertions....
Read more >
How to Use Shouldly to Improve Unit Tests in .NET?
Shouldly is a library that improves the quality of our tests in .NET. Here are some examples of how shouldly can help us...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found