简体   繁体   中英

How to Assert a list in unit test?

I have this test method where I am testing a list. It is working fine but I want to simplify the Assert.

<TestMethod()> Public Sub Method_Scenario_ReturnsList()
  'Arrange
    Dim sut As New ClassName()
  'Act
    Dim result = sut.ListMethod(parameter)
  'Assert
    Assert.AreEqual("1", result(0).ID)
    Assert.AreEqual("One", result(0).Name)
    Assert.AreEqual("2", result(1).ID)
    Assert.AreEqual("Two", result(1).Name)
End Sub

Is there a way to simplify this in just two lines of Assert where I check all the IDs and all the Names? Something like

CollectionAssert.AreEqual({"1","2"}, result.Select(Of ))

Is there a syntax to do that? I am noob in VB.net

You can use

CollectionAssert.AreEqual({"1","2"}, result.Select(Function(item) item.Id).ToArray())

CollectionAssert.AreEqual({"One","Two"}, result.Select(Function(item) item.Name).ToArray())

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