You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Question: How to throw Exceptions with FakeItEasy and the MockFileSystem #776
I have a MockFileSystem describing a file structure and want to check, that thrown IOExceptions during the deletion get catched by my software under test. Unfortunately I am not able to successfully initialize a MockFileSystem and throw a Exception on the call of File.Delete().
Code to test:
public IFileSystem FileSystem; // should be a MockFileSystem for Unit-Tests
public void DeleteUserData(string userId){
// parses the complete file structure, searches for different files etc. Finally finds the path.
var path = DoSomeMagicToFindTheCorrectPath(userId);
if(FileSystem.Directory.Exists(path){
//This line should throw an exception!
FileSystem.Directory.Delete(path, true);
}
Unittest:
[Test]
public void DeleteFiles([Values(IOException)] type exceptionType){
var FileSystemStructure = new Dictionary<string, MockFileData>(){
{"C:\UserDataDir\User1\filea.txt", new MockFileData("fileA")},
{"C:\UserDataDir\User2\filex.txt", new MockFileData("fileX")}
};
var fileSystem = A.Fake<MockFileSystem>(options=> options.CallsBaseMethods());
foreach (var v in FileSystemStructure)
{
fileSystem.AddFile(v.Key, v.Value);
}
A.CallTo(() => fileSystem.Directory.Delete(@"C:\UserDataDir\User1\").Throws(
() => (Exception) Activator.CreateInstance(exceptionType)
);
}
Output during execution:
System.ArgumentException : Object 'System.IO.Abstractions.TestingHelpers.MockDirectory' of type System.IO.Abstractions.TestingHelpers.MockDirectory is not recognized as a fake object.
I don't know FakeItEasy very well, but I guess you somehow have to fake the object graph "recursively". The error message indicates the the faked MockFileSystem returns a non-faked MockDirectory via its Directory property.
Yes, it might be, since FileSystem.GetDirectory() is not a virtual function and can as those not be mocked by FakeItEasy. Is Moq able to do so?
The code has to do several path operations before it determines, which paths should be deleted. That's, why your framework got so interesting: The ability to fake a file system and the chance to throw Exceptions for path/file system operations theirby.
Activity
fgreinacher commentedon Dec 10, 2021
I don't know
FakeItEasyvery well, but I guess you somehow have to fake the object graph "recursively". The error message indicates the the fakedMockFileSystemreturns a non-fakedMockDirectoryvia itsDirectoryproperty.StarWars999123 commentedon Dec 13, 2021
Yes, it might be, since FileSystem.GetDirectory() is not a virtual function and can as those not be mocked by FakeItEasy. Is Moq able to do so?
The code has to do several path operations before it determines, which paths should be deleted. That's, why your framework got so interesting: The ability to fake a file system and the chance to throw Exceptions for path/file system operations theirby.
fgreinacher commentedon Dec 17, 2021
AFAIK Moq can also just mock virtual members. I guess #757 would be of help here. What do you think @StarWars999123?
feat: allow overriding properties of (Mock)FileSystem
feat: allow overriding properties of (Mock)FileSystem (#782)
github-actions commentedon Dec 22, 2021
This is addressed in release v16.0.1.