views:

21

answers:

2

During doing unit tests in Rails a few temp files are created (associated with model). When test is done I want to remove these files, so I have to find a way to do this no matter the test passes or not. So, what are you suggestions?

+3  A: 

Why not put the file creation in a setup and the destruction in a teardown function? Then the creation will run beforehand - no matter what - and the destruction will run afterwards - again, no matter what. If you want setup and teardown of these files only for certain conditions, there's a nice writeup on it here: http://technicalpickles.com/posts/rails-special-sauce-test-unit-setup-and-teardown/

jasonpgignac
Thanks, it's exactly what I need.
jesper
+1  A: 

In a meanwhile I've tested something like this:

begin
  ...
  asserts
  ...
ensure
  delete_temporary_files
end

and it also works fine.

jesper