【问题标题】:FluentAssertion : Assert object equality with private inner list of objectFluentAssertion : 用私有内部对象列表断言对象相等
【发布时间】:2021-02-22 13:07:37
【问题描述】:

我尝试使用 FluentAssertion pass 进行测试,但我得到了著名的“System.InvalidOperationException : No members were found for comparison”,我不知道如何在这个特定的上下文中让它通过。

根比较对象类型有一个私有的内部对象列表(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);
    }

【问题讨论】:

    标签: c# .net unit-testing testing fluent-assertions


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 2022-05-01
      • 2022-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多