【问题标题】:How does FluentAssertions compare 2 objects (Using Reflection or another way)?FluentAssertions 如何比较 2 个对象(使用反射或其他方式)?
【发布时间】:2020-03-24 22:10:57
【问题描述】:

我目前正在使用FluentAssertion 来比较 2 个对象。

我真的很想知道它用什么来比较?

使用Reflection 然后像这样循环所有道具?

public static void PropertyValuesAreEquals(object actual, object expected)   
{
        PropertyInfo[] properties = expected.GetType().GetProperties();
        foreach (PropertyInfo property in properties)
        {
            object expectedValue = property.GetValue(expected, null);
            object actualValue = property.GetValue(actual, null);
          if (!Equals(expectedValue, actualValue))
                Assert.Fail("Property {0}.{1} does not match. Expected: {2} but was: {3}", property.DeclaringType.Name, property.Name, expectedValue, actualValue);
    //……………………………….
}

如果用另一种方式来比较,那是什么?

【问题讨论】:

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


    【解决方案1】:

    我建议您read the documentation 了解它使用的算法。但我可以告诉你,它充满了反射。

    【讨论】:

    • 首先,请允许我对您的回答表示感谢。但是,据我所知:反射机制并不像其他解决方案那样快实际上,我真的很想知道“对象图比较”和[“正常比较方式”之间有什么不同2个这样的对象”](ibb.co/3RWFLFB) 说说几个方面: 1 性能:哪个更好? 2 FluentAssertion 的方式:是什么让它比另一种方式更好?我的意思是每次我运行单元测试时,它是否都会像我上面提到的正常方式一样循环遍历所有属性以进行比较?
    • FA 做了深度比较。因此,它不仅会查看属性的值,还会查看这些属性的类型,并且会根据类型使用不同的策略。另见github.com/fluentassertions/fluentassertions/blob/master/Src/…
    猜你喜欢
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    相关资源
    最近更新 更多