© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
7 replies
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)
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);
        
    // ...
}
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
FileStream
instead of a
string
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Proper mocking for Unit Testing (NUnit)
C#CC# / help
4y ago
✅ Integration testing
C#CC# / help
16mo ago
Integration Testing
C#CC# / help
4y ago
✅ Testing Guidance (Unit/Integration)
C#CC# / help
4mo ago