1

I'm currently doing some unit testing of a copy function and I need to compare the elements of the objects between the old list, and the newly copied list. It works fine, but I was wondering if I can do it in a way that doesn't involve a for loop. Here is my object:

new NaturePointObject
            {
                SId = 1,
                Name = "Test",
                Category = NaturePointCategory.Category1,
                CreatorType = CreatorTypeEnum.1,
                NaturR = NaturR.Bn,
                Description = "Test",
                Kumulation = Kumulation.EnEjendom,
                Id = 1
            }

My old list contains "NaturePointObject" and is called naturPointList, and it will be copied to a list called newNaturePointList. Here is how I Assert to know if it copied succesfully:

Assert.AreEqual(naturPointList.Count,newNaturePointList.Count);
        for (var i = 0; i < newNatureList.Count; i++)
        {
            Assert.AreEqual(naturPointList[i].Category, newNaturePointList[i].Category);
            Assert.AreEqual(naturPointList[i].Description, newNaturePointList[i].Description);
            Assert.AreEqual(naturPointList[i].Kumulation, newNaturePointList[i].Kumulation);
            Assert.AreEqual(naturPointList[i].Name, newNaturePointList[i].Name);
            Assert.AreEqual(naturPointList[i].CreatorType, newNaturePointList[i].CreatorType);
            Assert.AreEqual(naturPointList[i].NaturR, newNaturePointList[i].NaturR);
            Assert.AreNotEqual(naturPointList[i].SId, newNaturePointList[i].SId);
        }

As you can see not all elements of the object must be equal. And I don't care about the "Id" of the object.

Is there a shorter way to do this, than run a for loop?

6
  • Shorter in which way? You can avoid the loop stuf with a LinQ query, but it will still create a loop under the hood. Commented Oct 24, 2017 at 13:02
  • Since you are literally doing an object compare(ignoring ID) you can take a look at this github.com/jamesfoster/DeepEqual it is available as nuget package. Commented Oct 24, 2017 at 13:04
  • @fharreau Just shorter to look at. It doesn't matter that it's the same under the hood. Commented Oct 24, 2017 at 13:12
  • @Aravind Will take a look at it Commented Oct 24, 2017 at 13:13
  • You stated that you don't care about the Id of the object, yet you have a specific assertion to ensure that they are not equal. It seems like you do care... Commented Oct 24, 2017 at 13:25

2 Answers 2

1

Probably you want to use CollectionAssert:

CollectionAssert.AreEqual(naturPointList, newNaturePointList, NaturePointObject.CategoryCreatorTypeComparer);

The only thing you need to take in mind is that you need to implement IComparer, to use in the Assert method:

public class NaturePointObject
{
    private static readonly Comparer<NaturePointObject> CategoryCreatorTypeComparerInstance = new CategoryCreatorTypeRelationalComparer();

    private sealed class CategoryCreatorTypeRelationalComparer : Comparer<NaturePointObject>
    {
        public override int Compare(NaturePointObject x, NaturePointObject y)
        {
            // compare fields which makes sense
            if (ReferenceEquals(x, y)) return 0;
            if (ReferenceEquals(null, y)) return 1;
            if (ReferenceEquals(null, x)) return -1;
            var categoryComparison = string.Compare(x.Category, y.Category, StringComparison.Ordinal);
            if (categoryComparison != 0) return categoryComparison;
            return string.Compare(x.CreatorType, y.CreatorType, StringComparison.Ordinal);
        }
    }

    public static Comparer<NaturePointObject> CategoryCreatorTypeComparer
    {
        get
        {
            return CategoryCreatorTypeComparerInstance;
        }
    }

    public int SId { get; set; }

    public string Category { get; set; }

    //other properties

    public string CreatorType { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can try

Assert.IsTrue(naturPointList.SequenceEqual(newNaturePointList));

If you want to ignore the Id, you can create other classes (without Ids).

Later edit: you could overwrite the Equals method and ignore the Id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.