简体   繁体   English

比较测试中的2个列表

[英]Compare 2 lists in testing

I am trying to make a test stub to check if the AddProduct method works or not. 我试图做一个测试存根,以检查AddProduct方法是否有效。

I am getting a list of all the Products from the database 我正在从数据库中获取所有产品的列表

List<Product> ProductsBeforePlusNewProd = new ProductsBL().getAllProducts().ToList();

Populating the new product to add 填充要添加的新产品

 Product newProduct = new Product();
        newProduct.ProductName = ...

Adding the newProduct to the database 将newProduct添加到数据库

new ProductsBL().addProduct(newProduct);

Adding the same product to the list ProductsBeforePlusNewProd which will hold the previous products and the new one - (this is the expected result) 将相同产品添加到ProductsBeforePlusNewProd列表中,该列表将保留先前的产品和新产品-(这是预期的结果)

getting the whole list after adding the product to database 将产品添加到数据库后获取整个列表

List<Product> ProductsAfter = new ProductsBL().getAllProducts().ToList();

Then this fails 然后这失败了

CollectionAssert.AreEqual(ProductsBeforePlusNewProd, ProductsAfter);

What am I missing here ? 我在这里想念什么? This is my first test stub 这是我的第一个测试存根

EDIT : Error message : CollectionAssert.AreEqual failed. 编辑:错误消息:CollectionAssert.AreEqual失败。 (Element at index 0 do not match.) (索引为0的元素不匹配。)

As pascx64 already showed, you can implement IEquatable at Product . 如pascx64所示,您可以在Product实现IEquatable But if you don't want to (or can't) change the implementation of Product , you could implement an IComparer : 但是,如果您不想(或不能)更改Product的实现,则可以实现IComparer

    public class ProductComparer : IComparer
    {
        public int Compare(object x, object y)
        {
            // compare the two objects
        }
    }

You can than use 您可以使用

CollectionAssert.AreEqual(ProductsBeforePlusNewProd, ProductsAfter, new ProductComparer());

This will compare the list instances, not the items. 这将比较列表实例,而不是项目。 I suggest you use IEnumerable.SequenceEquals (your item must implement equals) 我建议您使用IEnumerable.SequenceEquals(您的项目必须实现equals)

Two collections are equal if they have the same elements in the same order and quantity. 如果两个集合的元素具有相同的顺序和数量,则两个集合相等。 Elements are equal if their values are equal, not if they refer to the same object. 如果元素的值相等,则元素相等;如果它们引用相同的对象,则元素相等。 The values of elements are compared using Equals by default. 默认情况下,使用“等于”比较元素的值。 Are you sure that the elements in the both collections are equal? 您确定两个集合中的元素相等吗? You need to implement IEquatable in Product class. 您需要在Product类中实现IEquatable。

I think you need to implement IEquatable in your Product class : 我认为您需要在您的Product类中实现IEquatable:

public class Product : IEquatable
{
    public string ProductName;
    public bool Equals( object o )
    {
        Product other = o as Product;
        if( other == null ) 
             return false;
        return ProductName == other.ProductName;
    }
}

By default, the CollectionAssert.AreEquals will only check if the two references refer to the same object 默认情况下, CollectionAssert.AreEquals仅检查两个引用是否引用相同的对象

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

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