【问题标题】:Unit test not working correctly when comparing collections比较集合时单元测试无法正常工作
【发布时间】:2015-10-28 14:39:55
【问题描述】:

我有一个非常简单的方法可以查询数据库并返回一个值。代码如下:

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);
}

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

预期:&lt;System.Collections.Generic.List'1[System.Nullable'1[System.Int32]]&gt;.

实际:&lt;System.Collections.Generic.List'1[System.Nullable'1[System.Int32]]&gt;.

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

【问题讨论】:

  • AreEqual 比较的是引用而不是内容

标签: c# unit-testing mstest


【解决方案1】:

Assert.AreEqual 比较引用,而不是内容。您需要使用 CollectionAssert 类及其方法,例如 CollectionAssert.AreEquivalent

【讨论】:

  • 好吧,我知道了。。谢谢你。时间一到我会接受的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多