I'm trying to Moq an Azure CloudBlockBlob
and have it return a Stream
so that I can test whether my BlobStorage
repository is handling the output correctly.
But somehow the returned stream is always empty.
Unit test code:
//....
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write("sample data");
writer.Flush();
stream.Position = 0;
var blobMock = new Mock<CloudBlockBlob>(new Uri("http://tempuri.org/blob"));
blobMock
.Setup(m => m.ExistsAsync())
.ReturnsAsync(true);
blobMock
.Setup(m => m.DownloadToStreamAsync(It.IsAny<MemoryStream>()))
.Returns(Task.FromResult(stream));
//....
Repository code:
//....
var blob = GetContainer(container).GetBlockBlobReference(name);
if (await blob.ExistsAsync())
{
var ms = new MemoryStream();
await blob.DownloadToStreamAsync(ms);
ms.Seek(0, SeekOrigin.Begin);
return ms;
}
//....
So my returned MemoryStream ms
is always an empty stream and not the stream
object I'm using in my Moq Returns()
method.
How can I have that blob return my sample stream?
DownloadToStreamAsync
copying my sample stream over toms
then? – Grigson