gpt4 book ai didi

c# - 为什么 Assert.AreEqual(T obj1, Tobj2) 会因相同的字节数组而失败

转载 作者:IT王子 更新时间:2023-10-29 03:38:33 26 4
gpt4 key购买 nike

我在下面的代码段中有两个相同的字节数组:

    /// <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 会失败?

最佳答案

Assert.Equals 使用 Equals 方法进行测试,该方法默认使用引用相等性,并且由于它们是不同的对象,因此它们不相等。您需要比较数组中的每个字节并验证它们是否相等。一种方法是将它们转换为实现 ICollection 的东西并使用 CollectionAssert.AreEqual()相反。

关于c# - 为什么 Assert.AreEqual(T obj1, Tobj2) 会因相同的字节数组而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1375166/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com