3

使用FluentAssertion 3.1.229,你如何比较两个不同的内容MemoryStream

写入actualStream.Should().Be(expectedStream);会产生以下错误:

System.IO.MemoryStream
{
   CanRead = True
   CanSeek = True
   CanTimeout = False
   CanWrite = True
   Capacity = 8
   Length = 8
   Position = 0
   ReadTimeout = "[Property 'ReadTimeout' threw an exception: 'Exception has been thrown by the target of an invocation.']"
   WriteTimeout = "[Property 'WriteTimeout' threw an exception: 'Exception has been thrown by the target of an invocation.']"
}, but found 

System.IO.MemoryStream
{
   CanRead = True
   CanSeek = True
   CanTimeout = False
   CanWrite = True
   Capacity = 8
   Length = 8
   Position = 0
   ReadTimeout = "[Property 'ReadTimeout' threw an exception: 'Exception has been thrown by the target of an invocation.']"
   WriteTimeout = "[Property 'WriteTimeout' threw an exception: 'Exception has been thrown by the target of an invocation.']"
}.

是的,我可以使用 NUnit Assert.That(actualStream, Is.EqualTo(expectedStream));,但可以使用 FluentAssertions 吗?

谢谢。

4

2 回答 2

3

Maybe this would work for you?

actualStream.ToArray().Should().Be(expectedStream.ToArray());
于 2014-09-02T20:30:11.097 回答
0

您的 NUnit 解决方案也不起作用,因为MemoryStream' 的Equals实现不会进行逐字节比较。相反,使用

actualStream.GetBuffer().ShouldBeEquivalentTo(expectedStream.GetBuffer()).

GetBuffer返回对内部字节数组的引用并调用Should().Be()它会导致集合断言进行逐字节比较。

于 2014-09-03T07:08:16.877 回答