Skip to content

Question: How to throw Exceptions with FakeItEasy and the MockFileSystem #776

Closed

Description

@StarWars999123

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.

Activity

  1. fgreinacher commented on Dec 10, 2021

    @fgreinacher
    Contributor

    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.

  2. StarWars999123 commented on Dec 13, 2021

    @StarWars999123
    Author

    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.

  3. fgreinacher commented on Dec 17, 2021

    @fgreinacher
    Contributor

    AFAIK Moq can also just mock virtual members. I guess #757 would be of help here. What do you think @StarWars999123?

  4. github-actions commented on Dec 22, 2021

    @github-actions

    This is addressed in release v16.0.1.

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