Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
WhoAteDaCake opened this issue Dec 7, 2022 · 3 comments

Comments

@WhoAteDaCake
Copy link

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
Copy link
Author

WhoAteDaCake commented Dec 7, 2022

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 WhoAteDaCake changed the title [FluentResults.Extensions.FluentAssertions.Test] HaveMetadata behaves differently for same result types [FluentResults.Extensions.FluentAssertions.Test] HaveMetadata breaks when new string is used Dec 8, 2022
@altmann
Copy link
Owner

altmann commented Feb 4, 2023

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

@danila032040
Copy link

danila032040 commented May 30, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants