# Unit tests and destructive methods

**URL:** <https://www.ruby-forum.com/t/unit-tests-and-destructive-methods/112158>\
**Category:** Ruby\
**Created:** [August 15, 2007, 11:43pm UTC](https://www.ruby-forum.com/t/unit-tests-and-destructive-methods/112158 "2007-08-15T23:43:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 15, 2007, 11:43pm UTC](https://www.ruby-forum.com/t/unit-tests-and-destructive-methods/112158/1 "2007-08-15T23:43:57Z")

</div>

Hello all,

As my flac library approaches 1000 lines of code I decided it would be  
prudent  
to finally get around to writing some unit tests, which I have done.  
Now: I  
have created a ‘reference’ flac file with known values, which I then  
read to  
see if my lib parses it correctly.

That works great for the ‘read’ part of the library, however, I am now  
adding  
methods which write/rewrite values in the flac file (tags, padding  
blocks  
etc) making permanent changes in the file, and I am wondering how best  
to  
write tests for these.

I am thinking that I need to write code which makes a copy of the  
reference  
file, write the changes, read the copy to ensure the changes were  
written  
correctly, then delete the copy.

Does this seem reasonable? Is there a better way?

Thanks for consideration,  
-d

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 16, 2007, 12:01am UTC](https://www.ruby-forum.com/t/unit-tests-and-destructive-methods/112158/2 "2007-08-16T00:01:58Z")

</div>

On 8/15/07, darren kirby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello all,

Cheers!

> I am thinking that I need to write code which makes a copy of the reference  
> file, write the changes, read the copy to ensure the changes were written  
> correctly, then delete the copy.

That is one option. You could also look into mockfs – it’s a Ruby  
library that creates a mock file system in memory, and fakes out the  
Ruby kernel into using that for File.open, etc.

[http://mockfs.rubyforge.org/](http://mockfs.rubyforge.org/)

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 16, 2007, 4:28am UTC](https://www.ruby-forum.com/t/unit-tests-and-destructive-methods/112158/3 "2007-08-16T04:28:49Z")

</div>

On Aug 15, 2007, at 14:43, darren kirby wrote:

> methods which write/rewrite values in the flac file (tags, padding
> 
> Does this seem reasonable? Is there a better way?

require ‘fileutils’  
require ‘tmpdir’

class FlacTest \< Test::Unit::TestCase

def setup  
@tempdir = File.join Dir.tmpdir, “flac\_test\_#{$$}”  
FileUtils.mkdir\_p @tempdir  
end

def teardown  
FileUtils.rm\_rf @tempdir  
end

end

Should get you started.

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 16, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/unit-tests-and-destructive-methods/112158/4 "2007-08-16T21:41:02Z")

</div>

Thanks everybody. I will play with these suggestions and see what I can  
come  
up with. I like the idea of just writing the file in memory.

-d

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 16, 2007, 8:49am UTC](https://www.ruby-forum.com/t/unit-tests-and-destructive-methods/112158/5 "2007-08-16T08:49:16Z")

</div>

darren kirby wrote:

> write tests for these.
> 
> I am thinking that I need to write code which makes a copy of the reference  
> file, write the changes, read the copy to ensure the changes were written  
> correctly, then delete the copy.
> 
> Does this seem reasonable? Is there a better way?  
> In addition to the other suggestions, you could write to a StringIO  
> instead of to a File - that way you don’t need to worry about touching  
> the filesystem at all, and the changes stay in memory.
