Search code examples
c#asp.net-mvcunit-testingnunitfluent-assertions

How does FluentAssertions compare 2 objects (Using Reflection or another way)?


I'm currently using FluentAssertion for comparing 2 objects.

I really want to know what is the way it uses to compare?

Using Reflection then loop all props like this?

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);
    //……………………………….
}

If it uses another way to compare, what is it?


Solution

  • I suggest you read the documentation to understand the algorithm it uses. But I can tell you it's full of Reflection.