tpierrain/NFluent

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

Closed this issue · 2 comments

Bug Type
Please pick one:

  • a check failed to detect an error (false negative), i.e. a test is green while it should be red.
  • a check raised a non existing error (false positive), i.e a test is red while it should be green.
  • an error message is invalid/incomplete (please provide samples)
  • a ran into an exception.
  • other.

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):

  • NFluent version: [3.1.0]
  • Net Version: [Net Core 9.0]
  • IDE: [vs2022]

Additional context
Add any other context about the problem here.

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.

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