Reputation: 53
What is the correct way to create a mock of BlobBaseClient.DownloadTo(Stream)?
So far, I have this and unfortunately the stream I'm trying to read from eventually doesn't contain anything
var stream = new Mock<MemoryStream>();
var responseMock = new Mock<Response>();
var xmlContent = @"<?xml version='1.0'?>test data</xml>";
responseMock.Setup(r => r.Content).Returns(new BinaryData(xmlContent));
responseMock.Setup(r => r.ContentStream).Returns(stream.Object);
_blobClientMock.Setup(b => b.DownloadTo(It.IsAny<MemoryStream>())).Returns(responseMock.Object);
Upvotes: 0
Views: 291
Reputation: 2364
To DownloadTo() here. You should not be reading a response stream from this API. The method writes response data for you to the stream provided in the arguments. A mock of this method should write mock response data to stream provided in the argument, not return mock data. E.g.
byte[] xmlContent = System.Text.Encoding.UTF8.GetBytes(@"<?xml version='1.0'?>test data</xml>");
var responseMock = new Mock<Response>();
// add some headers to the response if desired
_blobClientMock.Setup(c => c.DownloadTo(It.IsAny<Stream>()))
.Callback((Stream s) => s.Write(xmlContent))
.Returns(responseMock);
Please treat as pseudocode
Upvotes: 1