C#C
C#4y ago
HimmDawg

Mocking FileStream for integration testing

I made a simple integration test that tests some functionality after calling an API endpoint. In my api client, I have this function UploadDocumentAsync(string filePath)

public async Task<RestResponse<int>> UploadDocumentAsync(string filePath)
{
    using var formData = new MultipartFormDataContent();

    var fileStream = new StreamContent(new FileStream(filePath));
    var fileName = Path.GetFileName(filePath);
        
    // ...
}


As you can see, this function expects a string as an argument. To make the function testable though, I'd need an abstraction for FileStream (which I know exists as a NuGet package) and inject it into the function. The question is: Is that better? I know it doesn't sound too bad, but calling the function with a FileStream instead of a string takes away a bit of comfort imo. I don't know if that's just silly goblinresHide . Or maybe you guys know another way to test such a function
Was this page helpful?