0

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.

1
  • 3
    AreEqual compares the references not the contents Commented Oct 28, 2015 at 14:42

1 Answer 1

6

Assert.AreEqual compares references, not the content. You need to use CollectionAssert class and its methods, like CollectionAssert.AreEquivalent

Sign up to request clarification or add additional context in comments.

1 Comment

Right, I got you.. Thanks for this. I'll accept it as soon as the time limit is up

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.