39

The following method fails:

[TestMethod]
public void VerifyArrays()
{
    int[] actualArray = { 1, 3, 7 };
    Assert.AreEqual(new int[] { 1, 3, 7 }, actualArray);
}

How do I make it pass without iterating over the collection?

Kevin Driedger
  • 51,492
  • 15
  • 48
  • 55
  • 2
    Why did you post a question only to answer it with 1 minute of googling? Why post the question at all? Or is this more of a PSA? – Jeremy Roberts Sep 23 '09 at 17:08
  • 4
    Public Service Announcement... hmm... Following the lead of Jeff Atwood on making SO the canonical place for questions and answers. – Kevin Driedger Sep 25 '09 at 13:24

2 Answers2

84

Microsoft has provided a helper class CollectionAssert.

[TestMethod]
public void VerifyArrays()
{
    int[] actualArray = { 1, 3, 7 };
    CollectionAssert.AreEqual(new int[] { 1, 3, 7 }, actualArray);
}
Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
Kevin Driedger
  • 51,492
  • 15
  • 48
  • 55
7

You can use the Enumerable.SequenceEqual() method.

[TestMethod]
public void VerifyArrays()
{
    int[] actualArray = { 1, 3, 7 };
    int[] expectedArray = { 1, 3, 7 };

    Assert.IsTrue(actualArray.SequenceEqual(expectedArray));
}
Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143