比较集合时单元测试无法正常工作

Unit test not working correctly when comparing collections

我有一个非常简单的方法来查询数据库和 returns 一个值。代码如下:

public List<int?> TravelTime()
{
   List<int?> items = new List<int?>();
   Induction induction = new Induction();
   using (var dbContext = new MyEntites())
   {
     var query = dbContext.MyTable.Select(a => a.Travel_Time).Take(1);

      foreach (var item in query)
      {
        induction.TravelTime = item;
        items.Add(induction.TravelTime);
      }
   }
   return items;// Value here is 8
}

我正在尝试使用以下代码对该方法进行单元测试:

[TestMethod]
public void Check_Travel_Time_Test()
{
  //Arrange
  InductionView vModel = new InductionView();
  Induction induction = new Induction();
  List<int?> actual = new List<int?>();
  induction.TravelTime = 8;
  actual.Add(induction.TravelTime);

  //Act
  var expected = vModel.TravelTime();

  //Assert
  Assert.AreEqual(expected, actual);
}

我不知道为什么它没有通过。我得到的例外是。

预计:<System.Collections.Generic.List'1[System.Nullable'1[System.Int32]]>.

实际:<System.Collections.Generic.List'1[System.Nullable'1[System.Int32]]>.

如果我进行调试,我的 TravelMethod 和测试方法中的值是正确的并且计数为 1。有人可以告诉我哪里出错了吗?预先感谢您的帮助。

Assert.AreEqual 比较参考文献,而不是内容。您需要使用 CollectionAssert class 及其方法,例如 CollectionAssert.AreEquivalent