AutoFixture Likeness - Compare Only Suitable Properties

I want to compare the following two objects for similarity using AutoFixture.SemanticComparison:

public class Object1 { public int a; } public class Object2 { public int a; public int b; } 

Now when I do it like this:

 var o1 = new Object1 { a = 1 }; var o2 = new Object2 { a = 1, b = 2}; o1.AsSource().OfLikeness<Object2>().ShouldEqual(o2); 

I get the following exception: "The following members did not match: - b."

I found out that I can omit the 'b' element as follows:

 var o1 = new Object1 { a = 1 }; var o2 = new Object2 { a = 1, b = 2}; o1.AsSource().OfLikeness<Object2>().Without(object2 => object2.b).ShouldEqual(o2); 

However, I find this rather cumbersome, because whenever I add a new element to the Object2 class, I have to fix my unit tests (or at least the unit test helpers).

Is there a way to say "I want to compare for similarity only for the subset that exists in both objects"?

+6
source share
1 answer

It looks like you would like to compare two objects based on the intersection of their properties. This class is currently not supported by the Likeness class. The rationale is as follows:

Currently, the type of destination (in the example above, which will be Object2) is the decisive template on which the mapping is performed. This gives a rather strong statement for the statement: each public property or field of this class must be mapped.

However, the statement that the intersection of properties coincides would be a very weak statement, because the intersection may be empty . This can lead to false negatives .

Even if you are TDDing and following the Red / Green / Refactor cycle, and you have seen a unit test with such a hypothetical Likeness intersection, subsequent refactorings can turn such a statement into False Negative, since you delete the Last property or field that has two objects. - and you will never notice.

However, AutoFixture is open source and that’s all, so you can offer this feature or send a transfer request.

+7
source

Source: https://habr.com/ru/post/908772/


All Articles