-
Notifications
You must be signed in to change notification settings - Fork 256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Providing mocks as and when necessary #1081
Comments
I am not sure, I understand your use case, @sameera. Do you use a mocking library like Moq or NSubstitute? If so, it should be easy to not use |
At a very high-level, the use case was to test that certain file operations are not being done over and over. So, we'd want our unit test to test that certain file system APIs (like File.ReadAllText) are not being called more than once. There are other file system operations along this execution path, so we don't want to have to replace all of them with mock code. |
The idea behind an In-Memory file system is, that it should behave (more or less) exactly like a real file system. If we would allow replacing single calls, this would result in much more complexity, as we could no longer rely on any consistent behavior. For this I would not like to introduce any (backward-incompatible) changes in the interface. That being said, if I understand your use case correctly, you would like some "analytical" information about which method is called how often. This could be an interesting extension to the current |
Closing the loop on this: // Setup a substitute to proxy MockFileSystem
_fileSystem = Substitute.ForPartsOf<MockFileSystem>();
// Setup a MockFile instance that we can observe
_fileSystem.File.Returns(Substitute.ForPartsOf<MockFile>(_fileSystem));
// ....
// Other setup and testing code
// ...
// Test (in this case, verify that a file read happened only once)
_fileSystem.File.Received(1).ReadAllText(Arg.Any<string>()); |
Problem:
I have a parser that iterates through the file system and parses each file one by one. The functionality is built around IEnumerables and needs to ensure that the next file is read for parsing only after all downstream functions are done processing the previous one.
To test for this, I want to setup a mock for
File.ReadAllText
and ensure that it's not being called until again the previous file is done.I could do this by setting up a mock for
IFile
and checking calls toReadAllText
as that's the only op I do onIFile
type. I do have few other calls to other file system objects that I don't want to mock/change. IfMockFileSystem.File
was settable, I can assign the mock for this test only without changing anything about the other tests.Proposed solution:
Provide setters for
MockFileSystem.File
andMockFileSystem.Directory
(for consistency and possible similar use cases).Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
Alternatives:
For such tests, setup a mocked
IFileSystem
from scratch with all objects and methods mocked. This could be tedious in many cases.The text was updated successfully, but these errors were encountered: