Skip to content

MockFileSystem sets Temp path to actual file system with no option to change this #588

@IrisClasson

Description

@IrisClasson

Apologies if this is intended behavior!

We've been using System.IO.Abstractions at work so we can increase our test coverage to include working with files on disk and the TestingHelpers let's us test more thoroughly that using mocks.

However, I came across a problem when it comes to working with files that are written to the temp directory.

The MockFileSystem implementation sets the IPath property in the constructor to a MockFile class that inherits from the MockWrapper class, that calls System.IOPath.GetTempPath(); The Path property has no setter, and I don't see any ways that this part can be mocked, nor can the temp path be changes. The MockFileSystem seems to mix a virtual and an actual system, and therefore there are several ways this can cause unnecessary exceptions.

Here is an example:

var mockFileSystem = new MockFileSystem();

var tempPath = mockFileSystem.Path.GetTempPath();

// Returns false
var exists = mockFileSystem.Directory.Exists(tempPath);

In our case, we write to the temp path, and the tests verify they can read from the same path. The Read* methods throw, as they verify the directory exists. The directory doesn't exist.

My workaround has been to get the temp path and add it as a directory to the MockFileSystem

var mockFileSystem = new MockFileSystem();

var tempPath = mockFileSystem.Path.GetTempPath();
mockFileSystem.AddDirectory(tempPath);

// Returns true
var exists = mockFileSystem.Directory.Exists(tempPath);

Should there a better way to do this (maybe there is and I've missed it)?

Thank you for the hard work btw, the libraries have been immensely useful! Would love to contribute to the project if there is anything up for grabs. Maybe docs?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testinghelpersIssues that address the testing helpersstate: ready to pickIssues that are ready for being worked ontype: bugIssues that describe misbehaving functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions