我有一个非常简单的方法,它查询数据库并返回一个值。守则如下:
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。有人能告诉我哪里出了问题吗?提前谢谢你的帮助。
发布于 2015-10-28 14:42:56
Assert.AreEqual比较引用,而不是内容。您需要使用https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.collectionassert.aspx类及其方法,如CollectionAssert.AreEquivalent
https://stackoverflow.com/questions/33394055
复制相似问题