Mocking Blobclient.downloadToasync (Stream) mit NSUBSubstitutC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Mocking Blobclient.downloadToasync (Stream) mit NSUBSubstitut

Post by Anonymous »

Ich habe eine andere Ausgabe -Antwort gesehen, die verwandt ist, aber ich kann meine Methode nicht verspottet. Ich möchte mehrere Tests erstellen. Ein Test für einen Download mit einer leeren Datei, eine Datei mit nur CSV -Headern und eine Datei mit CSV -Headern und eine Zeile. . Irgendwelche Vorschläge?

Code: Select all

// Create a memory stream to simulate the downloaded content
var memoryStream = new MemoryStream();
var writer = new StreamWriter(memoryStream);
writer.WriteLine("Header1,Header2,Header3");
writer.Flush();
//memoryStream.Position = 0;

var t = BlobsModelFactory.BlobDownloadStreamingResult(memoryStream);
var val = Response.FromValue(t, Substitute.For());
var returnVal = val.GetRawResponse();

blobClient.DownloadToAsync(Arg.Any()).Returns(returnVal);
< /code>
Ich denke, ich habe die richtige Blobsmodelfactory -Methode verwendet. Ich habe den Code von Microsoft debuggen und nach einem ähnlichen Modell in der Fabrik gesucht. < /P>
Der Code, den ich testen möchte, ist so etwas. Die Kunden werden in meine Klasse injiziert.public async Task GetMyDoc(MyCommand command)
{
var blobPath = command.blobPath;

// Get container client
var blobContainerClient = _blobServiceClient.GetBlobContainerClient(command.containerId);

// Get the blob client for the blob path.
var blobClient = blobContainerClient.GetBlobClient(blobPath);

using var fileStream = new MemoryStream();
await blobClient.DownloadToAsync(fileStream);

using var sr = new StreamReader(fileStream);
fileStream.Position = 0;

var rowNumber = 0;
while (!sr.EndOfStream)
{
// Other logic will be done here but for simplicity just count the number of rows...
rowNumber++;
}

return rowNumber;
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post