FluentResults icon indicating copy to clipboard operation
FluentResults copied to clipboard

[FluentResults.Extensions.FluentAssertions.Test] HaveMetadata breaks when `new string` is used

Open WhoAteDaCake opened this issue 3 years ago • 3 comments

The following fails in my XUnit test suite:

        
        var testValue = String.Join(", ", new [] {"test1"});
        var error = new Error("Missing columns from extraction");
        IEnumerable<(string, string)> groups = new (string, string)[] { ("Category", testValue) };
        foreach (var (key, value) in groups)
        {
            error = error.WithMetadata(key, value);
        }
        var testResult = Result.Fail<JObject>(error);
        
        // Act
        var result = sut.VerifyMapping(JsonConvert.SerializeObject(mapping), extraction);

        // Assert
        testResult.Should()
            .BeFailure()
            .And.HaveReason("Missing", MessageComparisonLogics.ActualContainsExpected)
            .That.HaveMetadata("Category", "test1");

        
        // Assert
        result.Should()
            .BeFailure()
            .And.HaveReason("Missing", MessageComparisonLogics.ActualContainsExpected)
            .That.HaveMetadata("Category", "test1");

Where result is the real value I'm trying to check and testResult is a variable I've created to try and duplicate. As you can see bellow they have the same structure.

image

However, the result assertions fail, but testResult don't. I've then tried to step through the actual actions of the assertion, where I've found the following issue (result assertion).

Screenshot from 2022-12-07 19-59-21

The equality comparison here returns false for "obj == metatdataValue, but when I tried obj.Equals(metatdataValue)` it returned true.

Do you have any idea why would this be happening, or what would be a way to fix it? Sorry if it's obvious, I'm quite new to dotnet

WhoAteDaCake avatar Dec 07 '22 20:12 WhoAteDaCake

Okay, so turns out, swapping out

var testValue = String.Join(", ", new [] {"test1"});
// To
var testValue = String.Join(", ", new [] {new string("test1")});
// Or
var testValue = String.Join(", ", new [] {"aatest1".Substring(2)});

Causes the testResult to fail as well. Is this intended behaviour?

WhoAteDaCake avatar Dec 07 '22 20:12 WhoAteDaCake

The second line is not buildable in my vs (.net 6). Which .net version do you use?

altmann avatar Feb 04 '23 17:02 altmann

I've got the same problem, but when using the Guid instead of the string. This error happens due to Guid boxing as the Guid equality operator compares objects rather than values.

image

As you can see here I have obj and metadataValue with the same value, but as Guid were boxed earlier they represent different objects. Using obj.Equals(metatdataValue) will resolve this issue, due to value comparison.

danila032040 avatar May 30 '23 10:05 danila032040