Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I verify that the expected array is the actual array in MSTest?

Tags:

c#

mstest

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?

like image 696
Kevin Driedger Avatar asked Sep 23 '09 16:09

Kevin Driedger


People also ask

How do you check if an array is empty in JavaScript?

We can verify that by checking the value of the Err object using this block of code: If Err <> 0 Then Wscript.Echo “This array is empty.” Err.Clear Else Wscript.Echo “This array is not empty.” End If

What does it mean when an array is not empty?

If Err equals 0, we echo back the fact that the array is not empty; remember, had the array been empty an error would have occurred and Err would be equal to something other than 0. (Any time Err equals 0 that means no error has occurred.) If Err is equal to something other than 0, that must mean the array is empty.

What happens if err is 0 in an array?

End If If Err equals 0, we echo back the fact that the array is not empty; remember, had the array been empty an error would have occurred and Err would be equal to something other than 0. (Any time Err equals 0 that means no error has occurred.)


Video Answer


2 Answers

Microsoft has provided a helper class CollectionAssert.

[TestMethod] public void VerifyArrays() {     int[] actualArray = { 1, 3, 7 };     CollectionAssert.AreEqual(new int[] { 1, 3, 7 }, actualArray); } 
like image 125
Kevin Driedger Avatar answered Sep 21 '22 21:09

Kevin Driedger


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)); } 
like image 22
Daniel Brückner Avatar answered Sep 21 '22 21:09

Daniel Brückner