43

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?

2
  • 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? Commented Sep 23, 2009 at 17:08
  • 5
    Public Service Announcement... hmm... Following the lead of Jeff Atwood on making SO the canonical place for questions and answers. Commented Sep 25, 2009 at 13:24

2 Answers 2

90

Microsoft has provided a helper class CollectionAssert.

[TestMethod]
public void VerifyArrays()
{
    int[] actualArray = { 1, 3, 7 };
    CollectionAssert.AreEqual(new int[] { 1, 3, 7 }, actualArray);
}
Sign up to request clarification or add additional context in comments.

Comments

9

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));
}

Comments

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.