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?
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
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.
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.)
Microsoft has provided a helper class CollectionAssert
.
[TestMethod] public void VerifyArrays() { int[] actualArray = { 1, 3, 7 }; CollectionAssert.AreEqual(new int[] { 1, 3, 7 }, actualArray); }
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)); }
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