Skip to content

Allow to use custom mocks for parts of the file system #1465

Description

@PaulMaget

Is your feature request related to a problem? Please describe.
For my tests I want to customize method returns, exceptions and count calls.
I generally do this using Moq, but using MockFileSystem makes it much more useful.

Describe the solution you'd like
My request would be to add a way to inject a different kind of mock in parts of the file system, or to add way to customize method callbacks.

Sorry if this already possible, I didn't find how.

Activity

  1. vbreuss commented on Apr 19, 2026

    @vbreuss
    Member

    I expect you read this in the documentation and it is not sufficient.
    Can you explain what is missing for your use case, so that I can think on what could be done?

  2. PaulMaget commented on Apr 19, 2026

    @PaulMaget
    Author

    Well with this I need to mock the behavior of every method.
    For my use case it would be much easier to only mock a part, like the IFile used by the mock, or even just the IFile.ReadAllText method.

  3. vbreuss commented on Apr 19, 2026

    @vbreuss
    Member

    The problem is, that then the file system would not get information about some interaction with it (as it is done on the mock). So it is essentially all or nothing here. You could check out Testably.Abstractions. It uses the same interface and has callbacks on the MockFileSystem:

    • MockFileSystem.Intercept to intercept events before they occur (you can then e.g. throw exceptions, or write a file)
    • MockFileSystem.Notify to react to events after they occurred.

    Alternatively you could write a MockableFileSystem that exposes all properties from the interface as virtual methods and forwards them to an internal MockFileSystem. Then you can use Moq (or any other mocking library) to mock your MockableFileSystem...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions