0

I'm starting with MS Test and trying to run following test. I see following part gives me a fail, which is quite suprising:

    [TestMethod]
    public void EachRetailerPassedToBuilderIsReturnedInVmList()
    {
        List<string> mockRetailers = new List<string> { "Asda", "Tesco" };

        Assert.AreEqual(new List<string>{ "Asda", "Tesco"}, mockRetailers);

    }

What am I doing wrong?

Result Message: Assert.AreEqual failed. Expected:.

Turo
  • 1,537
  • 2
  • 21
  • 42

1 Answers1

1

The problem is that List<T>.Equals does not provide elementwise check of data. It will only compare references. Quick solution would be to iterate through data and Assert.Equals on each element. Remember though to check if list sizes are equal beforehand.

It is also possible to use CollectionAssert.AreEqual to get appropriate result.

How to compare Lists in Unit Testing

Community
  • 1
  • 1
bottaio
  • 4,963
  • 3
  • 19
  • 43