C# Unit Test using FileStream

  Kiến thức lập trình

I had a service that would return an image’s data URLs, and I want to do unit test for it.

The following is part of its: (“path” parameter is a real path route)

...
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
    byte[] buffer = new byte[fileStream.Length];
    int bytesRead = fileStream.Read(buffer, 0, buffer.Length);

    if (bytesRead < buffer.Length)
    {
        return ...
    }   
    ...
    return ......
}      

So my question is, how can I mock FileStream without a real path? I used xUnit and Substitute and found that FileStream can use Substitute but not know why it needs a real path for the parameter. How else can I try? Is there any way except to use wrapper?

Thanks for any advice.

LEAVE A COMMENT