# Testing file system manipulations

**URL:** <https://www.ruby-forum.com/t/testing-file-system-manipulations/158964>\
**Category:** Ruby\
**Created:** [January 29, 2009, 9:40pm UTC](https://www.ruby-forum.com/t/testing-file-system-manipulations/158964 "2009-01-29T21:40:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cvincent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@cvincent](https://www.ruby-forum.com/u/cvincent)\
**Post date:** [January 29, 2009, 9:40pm UTC](https://www.ruby-forum.com/t/testing-file-system-manipulations/158964/1 "2009-01-29T21:40:04Z")

</div>

I am currently writing a class which processes a log file, inserts some  
data into the database, and then rotates the log file. I am doing  
test-driven development and I was curious as to the best practices for  
testing classes which manipulate the file system.

Rails tests have a transactional feature for testing database  
manipulation such that changes made to the DB are reset after each test.  
I was wondering if there was anything like this for the file system. It  
would be great if the test could automatically reverse any file system  
changes made during a test; I could imagine doing this via proxying  
access to the File, FileUtils, and Dir classes. Has anything like this  
been done? Or is there a better approach altogether?

For the record, I’m using Shoulda.

---

<div class="post-metadata">

**Author:** ![cvincent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@cvincent](https://www.ruby-forum.com/u/cvincent)\
**Post date:** [January 31, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/testing-file-system-manipulations/158964/2 "2009-01-31T14:19:37Z")

</div>

On Thu, Jan 29, 2009 at 10:37 PM, Chris Vincent  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> access to the File, FileUtils, and Dir classes. Has anything like this  
> been done? Or is there a better approach altogether?
> 
> ## For the record, I’m using Shoulda.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

You should use mock objects in place of file operations.  
If you’re using mocha, you can do things like

File.expects(:delete).with(file\_name)  
File.delete(file\_name)

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![cvincent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@cvincent](https://www.ruby-forum.com/u/cvincent)\
**Post date:** [January 31, 2009, 2:40pm UTC](https://www.ruby-forum.com/t/testing-file-system-manipulations/158964/3 "2009-01-31T14:40:22Z")

</div>

You can find Mocha here: [http://mocha.rubyforge.org/](http://mocha.rubyforge.org/)
