# Resetting fixtures for each iteration within a single test

**URL:** <https://www.ruby-forum.com/t/resetting-fixtures-for-each-iteration-within-a-single-test/120223>\
**Category:** Rails\
**Created:** [November 14, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/resetting-fixtures-for-each-iteration-within-a-single-test/120223 "2007-11-14T21:57:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bas\_van\_Westing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85f322/32.png) [@Bas\_van\_Westing](https://www.ruby-forum.com/u/Bas_van_Westing)\
**Post date:** [November 14, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/resetting-fixtures-for-each-iteration-within-a-single-test/120223/1 "2007-11-14T21:57:01Z")

</div>

Hi,

I want to reset the fixture data manually during a single test. I know  
this action is performed between tests, but I need to trigger it  
within a single test.

I need this because I have a directory full of test data (xml files)  
which need to be sent to the application and validated for their  
response. These files are all sent to a single controller action,  
which dispatches the request to the appropriate Restful controller/  
action. This controller mainly exists for backward compatibility  
reasons which do not map to Restful controllers.  
I already have tests on the Restful controller actions, but I want to  
run this dispatching test as well. So what I do now is create a single  
test which iterates over all these files and after each run reset! the  
session for the next run.

class DispatcherControllerTest \< Test::Unit::TestCase

def test\_all\_xml\_files  
all\_files.each do |file|  
post :submit, file  
…assertions…  
reset!  
end  
end

end

The problem is that the submitted files change the state of the  
application. I don’t want the tests to be dependent of each other so I  
want to reset the fixture data after each iteration, but I can’t find  
the method to do this.

Does anybody know how to reset the fixtures manually during a single  
test?

Thanks in advance,  
Bas van Westing

---

<div class="post-metadata">

**Author:** ![Bas\_van\_Westing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85f322/32.png) [@Bas\_van\_Westing](https://www.ruby-forum.com/u/Bas_van_Westing)\
**Post date:** [November 14, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/resetting-fixtures-for-each-iteration-within-a-single-test/120223/2 "2007-11-14T22:33:54Z")

</div>

On 14 Nov 2007, at 20:56, Bas van Westing wrote:

> the method to do this.
> 
> Does anybody know how to reset the fixtures manually during a single  
> test?

You need to rollback the current database transaction and start a new  
one.  
An alternative method would be to dynamically generate a testcase for  
each data file you have.

Fred

---

<div class="post-metadata">

**Author:** ![Bas\_van\_Westing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85f322/32.png) [@Bas\_van\_Westing](https://www.ruby-forum.com/u/Bas_van_Westing)\
**Post date:** [November 15, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/resetting-fixtures-for-each-iteration-within-a-single-test/120223/3 "2007-11-15T16:49:17Z")

</div>

Hi Fred,

Thanks for your response. Can you also give me the statement which  
rolls back the current database transaction?

Regards, Bas

On Nov 14, 10:33 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Bas\_van\_Westing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85f322/32.png) [@Bas\_van\_Westing](https://www.ruby-forum.com/u/Bas_van_Westing)\
**Post date:** [November 16, 2007, 10:07am UTC](https://www.ruby-forum.com/t/resetting-fixtures-for-each-iteration-within-a-single-test/120223/4 "2007-11-16T10:07:28Z")

</div>

On 15 Nov 2007, at 15:45, Bas van Westing wrote:

> Hi Fred,
> 
> Thanks for your response. Can you also give the statement for rolling  
> back the current database transaction?

ActiveRecord::Base.connection.rollback\_db\_transaction  
ActiveRecord::Base.connection.begin\_db\_transaction

Fred

---

<div class="post-metadata">

**Author:** ![Bas\_van\_Westing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85f322/32.png) [@Bas\_van\_Westing](https://www.ruby-forum.com/u/Bas_van_Westing)\
**Post date:** [November 16, 2007, 12:04pm UTC](https://www.ruby-forum.com/t/resetting-fixtures-for-each-iteration-within-a-single-test/120223/5 "2007-11-16T12:04:09Z")

</div>

Fred,

Thanks for your help. I ended up generating the test cases dynamically  
as you suggested.  
This gives nicer test outputs in case of failing test cases. Ruby is  
so beautiful…

class DispatcherControllerTest \< Test::Unit::TestCase

Dir.glob( “#{File.dirname( **FILE** )}/…/internal/\*input.xml” ).each  
do |input\_filename|  
output\_filename = input\_filename.gsub( ‘input.xml’, ‘output.xml’)  
next unless File.exists?( output\_filename )  
method\_name = “test\_internal\_#{File.basename(input\_filename,  
‘.xml’)}”  
define\_method( method\_name ) do  
…Actual method contents here, comparing the response with the  
expected output file…  
end  
end

end

On Nov 15, 5:04 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
