Skip to content

Use MockFileSystem together with mocking interfaces? #750

Closed

Description

@rklec

If you use MockFileSystem you AFAIK cannot use the second way described in the Readme with mocking the interfaces via a framework, such as NSubstitute, in my case. So you can only use one thing and they are exclusively to each other, are not they?

Or is there a way to use these, together? I.e. mock the basic file data via MockFileSystem and then just substitute some interfaces, where you usually need to trigger an access error, for a directory e.g..
In my case the suggested AllowedFileShare did not cause an error if I used it with MockDirectoryData(), so I think I need to us interface mocking here, unfortunately.

For this to work with NSubstitute, the MockFileSystem would have to return a Substitute.For<> each interface it creates, so that you can then use NSubstitute to further mock/restrict the interfaces.

See also nsubstitute/NSubstitute#659

Activity

  1. fgreinacher commented on Aug 26, 2021

    @fgreinacher
    Contributor

    Sorry for answering that late, I somehow missed the issue 🙈

    I don't know exactly how NSubsitute works, but I guess you could somehow create mocks/stubs at the interface level and delegate some mocked method to a MockFileSystem instance? If the framework does not support it you could implement it via the Decorator pattern.

    Does that help?

  2. rklec commented on Aug 30, 2021

    @rklec
    Author

    Oh of course, that could be possible. Thanks.
    It's still easier than the inverse way (i.e. mock everything and then try to substitute only parts) as you still need to delegate each called function (or thankfully, maybe also bigger objects like FileSystem.File if you don't need to mock that), but yeah, it's possible like that!

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