Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock filesystem in integration testing

I'm writing a ruby program that executes some external command-line utilities. How could I mock the filesystem from my rspec tests so that I could easily setup some file hierarchy and verify it after testing. It would also be best to be implemented in ram so that tests would run quickly.

I realize that I may not find a portable solution as my external utilities are native programs interacting directly with operating system file services. Linux is my primary platform and solution for that would suffice.

like image 253
JtR Avatar asked Nov 13 '08 09:11

JtR


People also ask

How do you mock a file system?

One option is to create a fake object that emulates the file system with an in-memory representation of it. The other possibility is to set up a test spy/mock for each test method. Considering the initial code snippet, both approaches are inappropriate. Let's see how dependency injection can help us.

What is mocking in integration testing?

Mocking in programming refers to an action of substituting a part of the software with its fake counterpart. Mocking technique is primarily used during testing, as it allows us to take out certain aspects of the tested system, thus narrowing the test's focus and decreasing the test's complexity.


2 Answers

Have you checked out FakeFS or MockFS?

Note: The original link to MockFS doesn't work. It looks like it's no longer being maintained.

like image 70
narsk Avatar answered Oct 14 '22 16:10

narsk


Maybe this won't answer your question directly, but in such cases I tend to create a temporary directory during test setup and remove it on teardown. Of course you also have to ensure the application writes to this temporary directory. I always have a configuration option defining destination directory that I can overwrite during testing.

When it comes to assertions I use plain File.exist? or File.directory?, but of course you can create your own wrappers around it. If you need some initial state you can build a directory that can be used as a fixture and will be copied to the temporary direcory during test setup.

like image 23
Adam Byrtek Avatar answered Oct 14 '22 16:10

Adam Byrtek