Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert that arrays are equal in Visual Studio 2008 test framework

Is there an easy way to check in a unit test that two arrays are equal (that is, have the same number of elements, and each element is the same?).

In Java, I would use assertArrayEquals (foo, bar);, but there seems to be no equivalent for C#. I tried Assert.AreEqual(new string[]{"a", "b"}, MyFunc("ab"));, but even though the function returns an array with "a", "b" the check still fails

This is using Visual Studio 2008 Team Suite, with the built-in unit test framework.

like image 348
Anteru Avatar asked May 22 '09 12:05

Anteru


People also ask

How do you assert equal in C#?

Assert. Equal(expected.Name, actual.Name); The first example fails due to the way comparison works for reference types. By default, the equality operation for those types will only assert whether the two objects being compared are the same, namely your variables are pointing to the same object within the memory heap.

Is assert equal?

assertEquals. Asserts that two objects are equal. If they are not, an AssertionError without a message is thrown. If expected and actual are null , they are considered equal.

What is assert in C# unit test?

The Arrange section of a unit test method initializes objects and sets the value of the data that is passed to the method under test. The Act section invokes the method under test with the arranged parameters. The Assert section verifies that the action of the method under test behaves as expected.


1 Answers

It's CollectionAssert.AreEqual, see also the documentation for CollectionAssert.

like image 199
Anteru Avatar answered Nov 10 '22 17:11

Anteru