Mock/Stub out filesystem in F# for unit testing I'm looking to do some basic verification testing on my functions that write to the filesystem.

I took a hint from here on how to mock out the filesystem using an interface, but I'm kinda bummed on how the FileSystemOperations type needs to be passed along through every function. Any ideas how to improve this, or where I went wrong?

I'm rather new to FP, so any feedback on other design issues are welcome as well.

module FileSystem =

    open System.IO

    type IFileSystemOperations = 
        abstract member copy: string -&gt; string -&gt; bool -&gt; unit
        abstract member delete: string -&gt; unit
        abstract member readAllBytes: string -&gt; byte[]
        abstract member createDirectory: string -&gt; DirectoryInfo
        abstract member directoryExists: string -&gt; bool

    type FileSystemOperations () =
        interface IFileSystemOperations with
            member this.copy source destination overwrite =
                File.Copy(source, destination, overwrite)

            member this.delete path =
                File.Delete path

            member this.readAllBytes path =
                File.ReadAllBytes path

            member this.createDirectory path =
                Directory.CreateDirectory path

            member this.directoryExists path =
                let fileInfo = FileInfo path
                fileInfo.Directory.Exists

module FileMover =

    open FileSystem

    let private ensureDirectoryExists (fileSystemsOperations:IFileSystemOperations) destination =
        let directoryExists = fileSystemsOperations.directoryExists destination
        if not (directoryExists) then
            fileSystemsOperations.createDirectory destination |&gt; ignore

    let private compareFiles (fileSystemsOperations:IFileSystemOperations) moveRequest =
        let sourceStream = fileSystemsOperations.readAllBytes moveRequest.Source
        let destinationStream = fileSystemsOperations.readAllBytes moveRequest.Destination
        sourceStream = destinationStream

    let private copyToDestination (fileSystemsOperations:IFileSystemOperations) (moveRequest:MoveRequest) =
        ensureDirectoryExists fileSystemsOperations moveRequest.Destination
        fileSystemsOperations.copy moveRequest.Source moveRequest.Destination true

    let private deleteSource (fileSystemsOperations:IFileSystemOperations) (move:Move) =
        fileSystemsOperations.delete move.Request.Source

    let private moveFile (fileSystemsOperations:IFileSystemOperations) moveRequest =
        copyToDestination fileSystemsOperations moveRequest
        let filesMatch = compareFiles fileSystemsOperations moveRequest
        let move = match filesMatch with
                   | true -&gt; {Request = moveRequest; Result = Success}
                   | false -&gt; {Request = moveRequest; Result = Failure BytesDidNotMatch}

        deleteSource fileSystemsOperations move

    let move (fileSystemsOperations:IFileSystemOperations) targetPath pictures =
        let moveFile = moveFile fileSystemsOperations

        pictures
        |&gt; getMoveRequests targetPath
        |&gt; Seq.map moveFile


Update (for reference &amp; anyone interested)

Based on the answers below, here is how I currently refactored the code. It's a work in progress, but it's already more easily testable (main goal) and the responsibilities seem to fit better as well (eg: injecting ensureDirectoryExists into copyToDestination instead of into moveFile).

Thanks for your answers!
Even though the SOLID principles are known to be principles related to Object-Oriented Design, I'd still take a cue from the Dependency Inversion Principle. As APPP states it (ch. 11): "clients [...] own the abstract interfaces".

You can apply this principle in FP as well, in the sense that you can start by defining the overall behaviour of the function you're interested in, and then see what happens.

As an example, I'd approach the ensureDirectoryExists function like this:

let private ensureDirectoryExists destination =
    let directoryExists = dirExists destination
    if not directoryExists then
        createDir destination


Now, the above version doesn't compile, because dirExists and createDir aren't defined. However, that's easy to fix: just promote them to function arguments:

let private ensureDirectoryExists dirExists createDir destination =
    let directoryExists = dirExists destination
    if not directoryExists then
        createDir destination


Viola: now you have a higher-order function; it's type is

('a -&gt; bool) -&gt; ('a -&gt; unit) -&gt; 'a -&gt; unit


No interfaces are required.

This is already intriguing for a couple of reasons:


It's no longer about files or directories: 'a could be string values containing file names or directory names, but it could also be DirectoryInfo or something not at all related to files.
Although it looks complex, it only involves two types: 'a and unit. That should make us pay attention as well.
You can use partial application to apply the ensureDirectoryExists with the two first arguments, and then use this partially applied function to test any number of different directories.


You can still 'implement' dirExists and createDir, and apply them like this:

open System.IO

let directoryExists path =
    let fileInfo = FileInfo path
    fileInfo.Directory.Exists

let ensureDirectoryExists' =
    ensureDirectoryExists directoryExists (Directory.CreateDirectory &gt;&gt; ignore)


Notice that the createDir 'implementation' is so simple that I chose to inline it. I could also have inlined the dirExists 'implementation' with (fun path -&gt; (FileInfo path).Directory.Exists), but I chose to do this as a separate function in order to illustrate the various options available.

The partially applied ensureDirectoryExists' function has this simple function signature: (string -&gt; unit), which is comparable to the OP version of the function.



While I think I'll leave the rest of the functions as an exercise, I'd like to return to the thought-provoking signature of the ensureDirectoryExists function:

('a -&gt; bool) -&gt; ('a -&gt; unit) -&gt; 'a -&gt; unit


There's nothing here about files or directories, so a more generic name might be in place. It looks like the Tester-Doer idiom, so we could call it testDo instead.

One of the great benefits of making the interface more generic is that you don't need to involve the file system at all for testing. Instead, you can test this function using integers, strings, or some other 'easy' values. If the logic is unconstrained generic and works for numbers and strings, it'll also work for directories and files.