Domanda

I have the following method, it is an extension method and can be called by any Stream object. The method should copy the exact content of a Stream into another Stream.

public static void CopyTo(this Stream input, Stream output)
{
   const int size = 10;
   int num;
   var buffer = new byte[size];
   input.Position = 0;
   while ((num = input.Read(buffer, 0, buffer.Length)) != 0)
   {
      output.Write(buffer, 0, num);
   }
}

I have created one simple test to verify that the content of the original Stream is equal to the content of the final Stream:

[TestMethod]
public void StreamWithContentShouldCopyToAnotherStream()
{
    // arrange
    var content = @"abcde12345";
    byte[] data = Encoding.Default.GetBytes(content);
    var stream = new MemoryStream(data);
    var expectedStream = new MemoryStream();
    // act
    stream.CopyTo(expectedStream);
    // assert
    expectedStream.Length
       .Should()
       .Be(stream.Length, "The length of the two streams should be the same");
}

Unfortunately I am covering just part of this method, because I don't verify if the content is exactly the same. Also dotCover is showing me that the first part of my code is not covered at all, which is this one:

Code coverage result

My target is 100% code coverage on this method.

È stato utile?

Soluzione

Perhaps your code is not calling your extension method but is instead calling the Stream.CopyTo(Stream) Method?

Try renaming your extension method to avoid the name clash.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top