Comparing Two objects using Assert.AreEqual()

后端 未结 5 1061
眼角桃花
眼角桃花 2020-12-30 20:24

I \'m writing test cases for the first time in visual studio c# i have a method that returns a list of objects and i want to compare it with another list of objects by using

5条回答
  •  悲哀的现实
    2020-12-30 21:04

    These answers are far too complicated for the issue. There are no overrides necessary to compare two Lists, and you do not need to break out multiple asserts. Microsoft uses the following class, CollectionAssert.

    CollectionAssert.AreEqual(expectedList, actualList)
    

    This works for Lists, Dictionaries, and whatever else implements ICollection interface.

    The microsoft documentation is at the following location and details the various types of assertions which can be made on collections

    http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.collectionassert.aspx

    However, as mentioned by @Bart, this does not work as expected on Lists of (complex) Objects, and the Equals method may still need to be overwritten for those cases.

提交回复
热议问题