[英]Unit test not working correctly when comparing collections
我有一个非常简单的方法来查询数据库并返回一个值。 代码如下:
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
和 Test 方法中的计数为 1。 有人能告诉我我哪里出错了吗? 在此先感谢您的帮助。
Assert.AreEqual比较引用,而不是内容。 您需要使用CollectionAssert类及其方法,例如CollectionAssert.AreEquivalent
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.