首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >单元测试在比较集合时不能正常工作

单元测试在比较集合时不能正常工作
EN

Stack Overflow用户
提问于 2015-10-28 14:39:55
回答 1查看 85关注 0票数 0

我有一个非常简单的方法,它查询数据库并返回一个值。守则如下:

代码语言:javascript
运行
复制
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
}

我试图用以下代码对此方法进行单元测试:

代码语言:javascript
运行
复制
[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。有人能告诉我哪里出了问题吗?提前谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-28 14:42:56

Assert.AreEqual比较引用,而不是内容。您需要使用https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.collectionassert.aspx类及其方法,如CollectionAssert.AreEquivalent

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33394055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档