Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

.net - Applying [AutoFixture] SemanticComparison OfLikeness to sequences / collections / arrays / IEnumerable

We have written a test which looks like the following. This test requires that we have created en Equal-overload for the CodeTableItem-class:

ICollection<CodeTableItem> expectedValutaList = new List<CodeTableItem>();
expectedValutaList.Add(new CodeTableItem("DKK", "DKK"));
expectedValutaList.Add(new CodeTableItem("EUR", "EUR"));
RepoDac target = new RepoDac(); 

var actual = target.GetValutaKd();

CollectionAssert.AreEqual(expectedValutaList.ToList(),actual.ToList());

The test works fine, but has the unfortunate dependency to the Equality-function, meaning if I extend the CodeTableItem-class with one more field, and forgets to extend the Equals-function, the unit test still runs green, although we do not test for all fields. We want to avoid this Equality pollution (see Test Specific Equality), which has been written only to conform to the test.

We have tried using OfLikeness, and have rewritten the test in this way:

ICollection<CodeTableItem> expectedValutaList = new List<CodeTableItem>();
expectedValutaList.Add(new CodeTableItem("DKK", "DKK"));
expectedValutaList.Add(new CodeTableItem("EUR", "EUR"));
var expectedValutaListWithLikeness = 
          expectedValutaList.AsSource().OfLikeness<List<CodeTableItem>>();

RepoDac target = new RepoDac(); 
ICollection<CodeTableItem> actual;

actual = target.GetValutaKd();

expectedValutaListWithLikeness.ShouldEqual(actual.ToList());

But the test fails because the Capacity is not equal. I have written code that runs through reflection many times, and typically ended up implementing overloads for ignoring fields. Is there a way to ignore certain fields with the OfLikeness or ShouldEqual? Or is there some other way of solving this issue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Why you don't want to do it like that

I don't think creating a Likeness from any List<T> does what you want it to do. As I understand, you want to compare the contents of two lists. That's not the same as comparing two lists...

Consider what Likeness does: it compares property values. What are the properties of List<T>?

They are

  • Capacity
  • Count

As Nikos Baxevanis points out in his answer, you can use the Without method to ignore the value of the Capacity property, but that means that only the Count property remains.

In other words, if you did that, this:

expectedValutaListWithLikeness.ShouldEqual(actual.ToList());

would be functionally equivalent to this:

Assert.AreEqual(expected.Count, actual.Count)

In other words, the lists could have totally different data, but the test would still pass if only each list has the same amount of elements. That's probably not what you want...

What you should do

You can use Likeness to compare each element against each other. Something like this should work:

var expectedValutaList = new List<CodeTableItem>();
expectedValutaList.Add(new CodeTableItem("DKK", "DKK"));
expectedValutaList.Add(new CodeTableItem("EUR", "EUR"));

var expectedValutaListWithLikeness = from cti in expectedValutaList
                                     select cti
                                         .AsSource()
                                         .OfLikeness<CodeTableItem>();

var target = new RepoDac(); 

var actual = target.GetValutaKd();

Assert.IsTrue(expectedValutaListWithLikeness.Cast<object>().SequenceEqual(
    actual.Cast<object>()));

You may also be able to use CollectionAssert for the assertion, but it's been so many years since I last used MSTest that I can't remember the quirks of that method...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...