MigMax
MigMax

Reputation: 80

FluentAssertion : Assert object equality with private inner list of object

I try to make a test using FluentAssertion pass but i get the famous "System.InvalidOperationException : No members were found for comparison" and i don't know how i can make it pass in this specific context.

The root compared object type has a private inner list of objects (Light) and i don't know how to write the config options object of BeEquivalentTo function.

    public class LightDashboard
    {
        private List<Light> _innerList;

        public LightDashboard(List<Light> innerList)
        {
            _innerList = innerList;
        }
   }



    public class Light
    {
        private bool _value;

        public Light(bool value)
        {
            _value = value;
        }
    }

And the test looks like :

    [Test]
    public void ListWithSameNumberOfElementsButDifferentValuesShouldNotBeEquivalent()
    {
        List<Light> sutInnerList = new List<Light>() {
            new Light(true)
        };
        _sutObject = new LightDashboard(sutInnerList);

        List<Light> expectedInnerList = new List<Light>(){
            new Light(false)
        };
        _expectedObject = new LightDashboard(expectedInnerList);

        _sutObject.Should().NotBeEquivalentTo(_expectedObject);
    }

Upvotes: 0

Views: 638

Answers (1)

Dennis Doomen
Dennis Doomen

Reputation: 8889

There's no magic for this. Your properties are private, hence FluentAssertions will ignore them. In fact, observing the internal details of an object in a test is IMHO bad practice. You should only assert the observable behavior of a class.

Upvotes: 1

Related Questions