# Test fixtures order

**URL:** <https://www.ruby-forum.com/t/test-fixtures-order/151370>\
**Category:** Rails\
**Created:** [October 20, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/test-fixtures-order/151370 "2008-10-20T19:59:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [October 20, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/test-fixtures-order/151370/1 "2008-10-20T19:59:06Z")

</div>

Hello, I’m trying to get my Rails test suite to load fixtures in a  
certain order, but I’m coming across a really strange problem.

I have two tables and a join table. Instead of creating the entries for  
the join table manually, I just go

Table1.each do |t1|  
Table2.each do |t2|  
…

to create the fields. This only works if Table1 and Table2 are loaded  
before the join table is loaded.

Now, when I go “rake test”, it fails on the unit tests that are  
dependent on the join table having values, but the functional tests that  
are dependent on them succeed. However, if I go “rake test:functionals”,  
they fail. It seems like the first test suite that gets loaded does not  
create these entries.

Anybody know why this might happen?

---

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [October 20, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/test-fixtures-order/151370/2 "2008-10-20T22:16:15Z")

</div>

On Oct 20, 6:59 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello, I’m trying to get my Rails test suite to load fixtures in a  
> certain order, but I’m coming across a really strange problem.
> 
> I have two tables and a join table. Instead of creating the entries for  
> the join table manually, I just go
> 
> Table1.each do |t1|  
> Table2.each do |t2|  
> …

Where is this code been run ? (and to be quite blunt - why?)

Fred

---

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [October 20, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/test-fixtures-order/151370/3 "2008-10-20T22:21:20Z")

</div>

> Where is this code been run ?

It’s being run in the join table’s fixture.

> (and to be quite blunt - why?)

Because there’s a lot of test data in the other two tables, and typing  
it out by hand would be big waste of time. Normally the data in the join  
table is generated by after\_save, but since that isn’t called when  
fixtures are loaded I have to do it this way.

---

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [October 22, 2008, 7:24am UTC](https://www.ruby-forum.com/t/test-fixtures-order/151370/4 "2008-10-22T07:24:03Z")

</div>

Great… what was your other solution?!

---

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [October 22, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/test-fixtures-order/151370/5 "2008-10-22T16:23:00Z")

</div>

I just wrote a script for runner that just loaded in all the original  
fixtures I had for the two non-join tables, and based on what was in  
those generated a YAML file for the join table and reloaded the DB.

---

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [October 20, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/test-fixtures-order/151370/6 "2008-10-20T22:39:18Z")

</div>

Actually never mind, I found another solution.
