为什么Assert.AreEqual(T obj1,Tobj2)在相同的字节数组下失败 [英] Why does Assert.AreEqual(T obj1, Tobj2) fail with identical byte arrays
问题描述
在下面的代码段中,我有两个相同的字节数组:
I have two identical byte arrays in the following segment of code:
/// <summary>
///A test for Bytes
///</summary>
[TestMethod()]
public void BytesTest() {
byte[] bytes = Encoding.UTF8.GetBytes(Properties.Resources.ExpectedPacketData);
TransferEventArgs target = new TransferEventArgs(bytes);
byte[] expected = Encoding.UTF8.GetBytes(Properties.Resources.ExpectedPacketValue);
byte[] actual;
actual = target.Bytes;
Assert.AreEqual(expected, actual);
}
两个数组都是相同的,直到字节为止.在这种情况下,为什么Assert.AreEqual失败?
Both arrays are identical down to the very byte. In this scenario, why would Assert.AreEqual fail?
推荐答案
Assert.Equals
使用Equals
方法进行测试,该方法默认情况下使用引用相等性,并且由于它们是不同的对象,因此它们不相等.您需要比较数组中的每个字节,并验证它们是否相等.一种方法是将它们转换为实现ICollection的东西,并使用 CollectionAssert.AreEqual().
Assert.Equals
tests using the Equals
method, which by default uses reference equality and, since they are different objects, they are not equal. You'll want to compare each byte in the array and verify that they are equal. One way to do this is convert them to something that implements ICollection and use CollectionAssert.AreEqual() instead.
这篇关于为什么Assert.AreEqual(T obj1,Tobj2)在相同的字节数组下失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!