1

I'm looking for a way to mock a filesystem in Scala. I'd like to do something like this:

class MyMainClass(fs: FileSystem) {
   ...
}

normal runtime:

val fs = FileSystem.default
main = new MyMainClass(fs)

test time:

val fs = new RamFileSystem
main = new MyMainClass(fs)

My examples look a lot like Scala-IO and I thought it might be my answer. However, it doesn't look like all the core functionality in Scala-IO works with the FileSystem abstraction. In particular, I can't read from a Path or apply Path.asInput. Further, several of the abstractions like Path and Resource seem tightly bound to FileSystem.default.

I also googled some interesting stuff in Scala-Tools, but that project seems to be defunct.

Rob

4

1 に答える 1

2

1 つのオプションは、独自の抽象化を作成することです。このようなもの:

trait MyFileSystem { def getPath() }

次に、実際の FileSystem とモック バージョンの両方で実装できます。

class RealFileSystem(fs: FileSystem) extends MyFileSystem {
  def getPath() = fs.getPath()
}

class FakeFileSystem extends MyFileSystem {
  def getPath() = "/"
}

そして、MyMainClass は FileSystem の代わりに MyFileSystem を要求できます

class MyMainClass(fs: MyFileSystem)
main = new MyMainClass(new RealFileSystem(FileSystem.default))
test = new MyMainClass(new FakeFileSystem)
于 2013-03-07T03:38:33.480 に答える