Why does Assert.AreEqual(T obj1, Tobj2) fail with identical byte arrays

前端 未结 6 2007
日久生厌
日久生厌 2021-02-03 16:56

I have two identical byte arrays in the following segment of code:

    /// 
    ///A test for Bytes
    ///
    [TestMethod()]
            


        
6条回答
  •  北海茫月
    2021-02-03 17:12

    Because arrays don't override Equals.

    You haven't said which test framework you're using, but basically it would be up to that framework to special-case arrays. You can always implement your own helper method to do that, of course. I've done that sometimes. For a quick and dirty hack, if you're using .NET 3.5 you can use the Enumerable.SequenceEqual extension method:

    Assert.IsTrue(actual.SequenceEqual(expected));
    

    A custom helper method could give you more details about how they differ, of course. You might find the methods in MoreLINQ.TestExtensions helpful, although they're fairly rough and ready too.

提交回复
热议问题