如何使用 FluentAssertions 比较两个 MemoryStream

dst*_*stj 6 fluent-assertions

使用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.']"
}.
Run Code Online (Sandbox Code Playgroud)

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

谢谢。

小智 10

也许这对你有用?

actualStream.ToArray().Should().Be(expectedStream.ToArray());
Run Code Online (Sandbox Code Playgroud)

  • .Be() 在 byte[] 上不可用(无论如何版本为 3.1.229)。但使用 Equal() 就可以了。 (2认同)