tpierrain / NFluent

Smooth your .NET TDD experience with NFluent! NFluent is an ergonomic assertion library which aims to fluent your .NET TDD experience (based on simple Check.That() assertion statements). NFluent aims your tests to be fluent to write (with a super-duper-happy 'dot' auto-completion experience), fluent to read (i.e. as close as possible to plain English expression), but also fluent to troubleshoot, in a less-error-prone way comparing to the classical .NET test frameworks. NFluent is also directly inspired by the awesome Java FEST Fluent assertion/reflection library (http://fest.easytesting.org/)
Apache License 2.0
310 stars 53 forks source link

Check with .HasFieldsWithSameValues fails because it is comparing collections non public member "_version". #346

Closed jmlaurenti closed 3 days ago

jmlaurenti commented 3 days ago

Bug Type Please pick one:

Describe the bug Comparing 2 collections (List) containing objects with same values is raising an error because the collection non-public member "_version" (which is incremented when adding/removing items) is different between the 2 collections it similar items are not added exactly in the same manner

To Reproduce

        var a = new List<Contact>(new Contact[]{
            new Contact { Name = "MisterA" }, 
            new Contact { Name = "MisterB" }
        });   // a._version=0

        var b = new List<Contact>(new Contact[]{
            new Contact { Name = "MisterA" },
            new Contact { Name = "MisterB" }
        }); // b._version=0
        Check.That(a).HasFieldsWithSameValues(b); // PASS

        var c = new List<Contact>(); // c._version=0
        c.Add(new Contact { Name = "MisterA" });  // c._version=1
        c.Add(new Contact { Name = "MisterB" });  // c._version=2

        Check.That(a).HasFieldsWithSameValues(c); // FAIL : a._version = 0 / c._version= 2

Expected behavior Non Public members should not be considered when comparing field values.

Screenshots image

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

dupdob commented 3 days ago

Hi Thanks for opening this issue, but HasFieldsWithSameValues works as designed, here, for better or for worse. I am not sure what you are trying to test for here, but you should expect to get the good result simply by using Check.That(a).IsEqualTo(b). If you still need fine control about the test, you can look into Considering. You can see #119 for context and history regarding HasFieldsWithSameValues. We will not change the behavior of an old check as it will introduce a breaking change. So I close this issue.

Feel free to open a new one or reopen this one if you are still unable to get the desired behavior via previous suggestions.

jmlaurenti commented 3 days ago

Hi @dupdob , thanks a lot for your prompt reply. I'm validating a serialize/deserialize process, and want to compare the source List with the deserialized collection, ensuring both collections contain objects (of type Contact) having the same values (while being of course different instances).

I was surprised to see that the way items are added to the list is influencing the NFluent test result, just because of the _version field which is used to prevent list modification while enumerating (if I understand correctly).

Anyway, I do understand now the history of that feature, and agree that changing its behavior is a bad idea. I'll find a way !

Have a great day, JM