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)
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 . Or maybe you guys know another way to test such a function6 Replies
i think the simplest way would be to just create a temp file with the desired content and pass its path to the method
Hmmm, in that case I should just create some random bytes as data and pass that. I don't wanna bother whether the file was really created etc 😄
well, the only other way i can think of would be creating a
public async Task<RestResponse<int>> UploadDocumentAsync(string fileName, Stream stream);
that does the actual work and which is called by ur current implementation.
then u could use for example a MemoryStream
instead of a real fileRight, I fiddled around a bit and just used
System.IO.Abstractions
and System.IO.Abstractions.TestingHelpers
I injected an IFileSystem
into my ApiClient
and changed the method to
And in my test, I created a MockFileSystem
which implements IMockFileDataAccessor
, which implements IFileSystem
Just if somebody has a similar issue That command had an error
UnknownCommand: Unknown command.
Remove your reaction to delete this message
oh well ¯\_(ツ)_/¯