Forum: Ruby Testing file system manipulations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
11611e595f8866809b075a8e718e7600?d=identicon&s=25 Chris Vincent (cvincent)
on 2009-01-29 21:40
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.
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-01-31 14:19
(Received via mailing list)
On Thu, Jan 29, 2009 at 10:37 PM, Chris Vincent
<c.j.vincent@gmail.com>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/.
>
>
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 Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
82a61255b4090da7d3619f870b0794dc?d=identicon&s=25 James Mead (floehopper)
on 2009-01-31 14:40
(Received via mailing list)
You can find Mocha here: http://mocha.rubyforge.org/
This topic is locked and can not be replied to.