如何比较两个MemoryStream和FluentAssertions

时间:2014-09-02 20:09:53

标签: 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.']"
}.

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

感谢。

2 个答案:

答案 0 :(得分:2)

也许这对你有用吗?

actualStream.ToArray().Should().Be(expectedStream.ToArray());

答案 1 :(得分:0)

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

Equals

actualStream.GetBuffer().ShouldBeEquivalentTo(expectedStream.GetBuffer())返回对内部字节数组的引用,并在其上调用GetBuffer将导致集合断言进行逐字节比较。