tpierrain/NFluent

NullReferenceException on HasFieldsWithSameValues

Closed this issue · 2 comments

Hi,

I'm facing a problem when using the Check.HasFieldsWithSameValues. When I use this method on a specific case I've got a NullReferenceException.

Here is a simple class :

public class FakeClass
{
    public List<string> Items { get; set; }
}

I try to compare two instances.
The first one's property has a value, the other one hasn't.

When checking as below, I've got a FluentCheckException, which is normal.
Check.That(instance1).HasFieldsWithSameValues(instance2);

But when checking the other way, I've got a NullReferenceException, which is not normal.
Check.That(instance2).HasFieldsWithSameValues(instance1);

Here is the full code :

[TestMethod]
public void HasFieldTestsMethod()
{
    var instance1 = new FakeClass()
    {
        Items = new List<string>()
    };
    var instance2 = new FakeClass();


    Check.That(instance2).HasFieldsWithSameValues(instance1); // throws NullReferenceException
    Check.That(instance1).HasFieldsWithSameValues(instance2); // throws FluentCheckException
}

The real problem is that using the same code as shown with a class as below will throw a NullReferenceException, but both instances should be considered as equals.

    public class FakeClass
    {
        private List<string> _Items;
        public List<string> Items
        {
            get
            {
                if (_Items == null)
                {
                    _Items = new List<string>();
                }
                return _Items;
            }
            set { _Items = value; }
        }
    }

thanks for taking the time for reporting this issue. I have been able to reproduce. I will first improve the NullRefExc situation by securing an adequate message, if necessary.

I just made a PR with a fix for the NullRefException.
I will revert shortly for the second part of your request