I have a very simple method which queries the database and returns a value. The code is as follows:
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
}
I'm trying to unit test this method with the following code:
[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);
}
I don't know why it's not passing. The exception I get is.
Expected:<System.Collections.Generic.List'1[System.Nullable'1[System.Int32]]>.
Actual:<System.Collections.Generic.List'1[System.Nullable'1[System.Int32]]>
.
If I debug I have the correct values and count is 1 in My TravelMethod
and Test method. Can someone tell me where I'm going wrong? Thanks in advance for your help.
Assert.AreEqual compares references, not the content. You need to use CollectionAssert class and its methods, like CollectionAssert.AreEquivalent