NUnit comparing two lists

后端 未结 6 452
孤独总比滥情好
孤独总比滥情好 2021-01-01 10:16

OK so I\'m fairly new to unit testing and everything is going well until now. I\'m simplifying my problem here, but basically I have the following:

[Test]
pu         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 11:08

    If you're comparing two lists, you should use test using collection constraints.

    Assert.That(actual, Is.EquivalentTo(expected));
    

    Also, in your classes, you will need to override the Equals method, otherwise like gleng stated, the items in the list are still going to be compared based on reference.

    Simple override example:

    public class Example
    {
        public int ID { get; set; }
    
        public override bool Equals(object obj)
        {
            return this.ID == (obj as Example).ID;
        }
    }
    

提交回复
热议问题