# Testing deleting files

**URL:** <https://www.ruby-forum.com/t/testing-deleting-files/64998>\
**Category:** Rails\
**Created:** [August 15, 2006, 4:41pm UTC](https://www.ruby-forum.com/t/testing-deleting-files/64998 "2006-08-15T16:41:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![reynardmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@reynardmh](https://www.ruby-forum.com/u/reynardmh)\
**Post date:** [August 15, 2006, 4:41pm UTC](https://www.ruby-forum.com/t/testing-deleting-files/64998/1 "2006-08-15T16:41:38Z")

</div>

In unit or functional test, is there a way to check that a file has been  
deleted?  
It seems that running the test does not really execute deleting files,  
which  
is good because I don’t want it to actually delete the files. But can I  
check that the deletion should have been performed in test environment?

thanks,

- reynard

---

<div class="post-metadata">

**Author:** ![reynardmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@reynardmh](https://www.ruby-forum.com/u/reynardmh)\
**Post date:** [August 15, 2006, 5:53pm UTC](https://www.ruby-forum.com/t/testing-deleting-files/64998/2 "2006-08-15T17:53:56Z")

</div>

Correction, running the test actually deletes the file ☹  
I think we want it to not delete the file but be able to check if the  
deletion has been performed, right? maybe I’ll try creating mock object  
for  
the File class.  
I’m sure someone else has encountered this need too. any suggestion?

thanks

- reynard

---

<div class="post-metadata">

**Author:** ![reynardmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@reynardmh](https://www.ruby-forum.com/u/reynardmh)\
**Post date:** [August 15, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/testing-deleting-files/64998/3 "2006-08-15T18:39:05Z")

</div>

On 15/08/06, Reynard H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Correction, running the test actually deletes the file ☹  
> I think we want it to not delete the file but be able to check if the deletion has been performed, right? maybe I’ll try creating mock object for the File class.  
> I’m sure someone else has encountered this need too. any suggestion?

You could use the Stubba component of Mocha  
([http://mocha.rubyforge.org](http://mocha.rubyforge.org)) to do something like the following…

def test\_should\_delete\_file  
File.expects(:delete).with(‘filename’).returns(1)  
object = MyClass.new  
object.method\_which\_deletes\_file  
File.verify  
end

This test should pass as long as the expected call to the File.delete  
method is made with the expected parameters. It will not actually  
delete the file.

Make sense?

James.

> **[James Mead](https://jamesmead.org/)**
>
> Ruby Developer, author of Mocha, and ex-Antarctic Hero.

---

<div class="post-metadata">

**Author:** ![reynardmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@reynardmh](https://www.ruby-forum.com/u/reynardmh)\
**Post date:** [August 15, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/testing-deleting-files/64998/4 "2006-08-15T19:54:15Z")

</div>

Thanks James, that’s exactly what I need.

- reynard

You could use the Stubba component of Mocha
