作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我在下面的代码段中有两个相同的字节数组:
/// <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/
我是一名优秀的程序员,十分优秀!