FluentAssertion :断言对象与对象的私有内部列表相等

FluentAssertion : Assert object equality with private inner list of object

我尝试使用 FluentAssertion pass 进行测试,但我得到了著名的“System.InvalidOperationException:没有找到要比较的成员”,我不知道如何让它在这个特定的上下文中通过。

根比较对象类型有一个私有内部对象列表 (Light),我不知道如何编写 BeEquivalentTo 函数的配置选项对象。

    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;
        }
    }

测试看起来像:

    [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);
    }

这没有魔法。您的属性是 private,因此 FluentAssertions 将忽略它们。事实上,在测试中观察对象的内部细节是恕我直言的坏习惯。您应该只断言 class.

的可观察行为