1

I'm generating a C# solution using another project. The generated solution contains multiple classes and a unit test file for each class.

A unit test creates two instances of a class and compares them. For the comparison, I chose FluentAssertion (I tried other external comparators like ComareNetObjects, but it took too long on objects with large depth).

The instances are compared as following:

inst1.Should().BeEquivalentTo(inst2);

The problem is that some of the generated classes have no fields or properties. For those classes, the unit test creates two instances (with no members) and compares them. The equivalency check throws:

System.InvalidOperationException(0x80131509): No members were found for comparison. Please specify some members to include in the comparison or choose a more meaningful assertion.

I don't want to expand the generated classes only for the tests, nor check for each instance's properties.

Is there a way to customize this behavior in FluentAssertion?

rotem agel
  • 11
  • 3
  • 1
    What do you mean with "the objects are empty"? Are they null? In that case, you can use `inst1.Should().NotBeNull()` before your check. – SomeBody Mar 02 '21 at 10:03
  • Did you go through https://fluentassertions.com/objectgraphs/ ? – Fildor Mar 02 '21 at 10:05
  • `A unit test creates two instances` how created instance could be null? – Renat Mar 02 '21 at 10:05
  • Are those objects the same type? Do they have matching Porperty names? Are all properties private? I think we need some more context. – Fildor Mar 02 '21 at 10:07
  • I meant empty objects as objects with no properties or fields, as the exception state 'no members were found'. I edited the question. – rotem agel Mar 02 '21 at 10:24
  • 2
    Quick & Dirty fix: Maye `inst1.GetType().GetProperties().Should().NotBeEmpty()` before the check works for you? – SomeBody Mar 02 '21 at 10:30
  • @SomeBody, most of the objects does have members, so I rather not – rotem agel Mar 02 '21 at 10:34
  • Can you describe how you want to compare types without any properties? – Jonas Nyrup Mar 02 '21 at 12:40
  • @JonasNyrup, I think type comparison is the first thing you check in equivalency, properties after. I think I would like the same here. A comparison that doesn't break on instances with no members.. – rotem agel Mar 02 '21 at 21:04

0 Answers0