简体   繁体   中英

How to compare 2 lists containing json values in C# for unit testing

How to compare 2 lists in c#? 1st list data

DBvalues = [{ "_id" : ObjectId("5d98298f9110f5f13812f3c1"), "PartName" : null, "MAP" : null, "MPN" : null, "IsActive" : true, "Gtin" : "ab085347019069" }, { "_id" : ObjectId("5d98298f9110f5f13812f3c0"), "PartName" : null, "MAP" : null, "MPN" : null, "IsActive" : true, "Gtin" : "085347014064" }]

2nd list data

actualExcelvalues = [{ "_id" : ObjectId("5d98298f9110f5f13812f3c0"), "PartName" : null, "MAP" : null, "MPN" : null, "IsActive" : true, "Gtin" : "085347014064" }, { "_id" : ObjectId("5d98298f9110f5f13812f3c1"), "PartName" : null, "MAP" : null, "MPN" : null, "IsActive" : true, "Gtin" : "ab085347019069" }]

irrespective of their order/index.

If you mean the strings then AreEqual works.

If you mean the objects then my technique for testing is to serialise both to indented json and compare that. Crude, but does the job.


In my current setup I have a static AreEqual<T>(T expected, T actual) which serialises both and then compares line by line to show better output when comparing fails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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