简体   繁体   English

断言等于两个对象列表 UnitTesting c#

[英]Assert Equal two list of objects UnitTesting c#

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.它工作正常,但我想知道我是否可以以不涉及 for 循环的方式来做到这一点。 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.我的旧列表包含“NaturePointObject”,名为 naturPointList,它将被复制到名为 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.而且我不关心对象的“Id”。

Is there a shorter way to do this, than run a for loop?有没有比运行 for 循环更短的方法来做到这一点?

Probably you want to use CollectionAssert :可能你想使用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:您唯一需要记住的是,您需要实现IComparer ,以在Assert方法中使用:

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; }
}

You can try你可以试试

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

If you want to ignore the Id, you can create other classes (without Ids).如果您想忽略 Id,您可以创建其他类(不带 Id)。

Later edit: you could overwrite the Equals method and ignore the Id.稍后编辑:您可以覆盖 Equals 方法并忽略 Id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM