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.
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.
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.
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.
It's CollectionAssert.AreEqual
, see also the documentation for CollectionAssert.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With