NUnit comparing two lists

后端 未结 6 498
孤独总比滥情好
孤独总比滥情好 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:02

    A very simple way to get this test to work is to only create the MyOtherClass instance once. That way, when comparing the item in the two lists they will be "equal" (because they reference the same object). If you do this, CollectionAssert will work just fine.

    [Test]
    public void ListTest()
    {
        var thing = new MyOtherClass();
    
        var expected = new List();
        expected.Add(thing);
    
        var actual = new List();
        actual.Add(thing);
    
        CollectionAssert.AreEqual(expected,actual);
    }
    

    If you don't this though, you'll need to implement IEquatable in MyOtherClass or override Equals to define what makes two instances of that class the "same".

提交回复
热议问题