2 different fixtures for a single database table?

0 views
Skip to first unread message

Cyrille

unread,
Feb 15, 2007, 4:40:13 AM2/15/07
to WellRailed
Hi guys,

I need to test this complicated model that acts as a tree. I have a
fairly complex fixture for it with many tests happily running on it.

Now I need a second, completely different fixtures to test the same
model.

I tried to name it: items2.yml

And then I create items2_test.rb and call:

fixtures :items2

But it fails saying: table "items2" does not exist. It seems that the
"fixtures" call is trying to delete all elements from a table with the
same name.

Is there a way to have 2 different fixtures for the same database
table (and model)?

Cheers

Cyrille

Henry Maddocks

unread,
Feb 15, 2007, 4:54:07 AM2/15/07
to WellR...@googlegroups.com
Just create a second set of data in the same file, eg...

first_one:
id: 1
some_field: "Some data"
another_field: 69
second_one:
id: 2
some_field: "Some different data"

Cyrille

unread,
Feb 15, 2007, 5:09:13 AM2/15/07
to WellRailed
Hi Henry,

Thanks for your response.

I need to give you a bit more details:

I am testing a model that acts as a tree and I need different
combinations.

For instance, in my first set of data, I have a root, then 3 children.

I would like to test with a root and only 2 children.

I just found a patch that would solve my problem: http://
dev.rubyonrails.org/ticket/1911

It adds a keyword fixture that can be used as:

fixture :items2, :table_name => "items", :file_name => "items2"

Unfortunately, it was apparently not included in Rails 1.2.1. Hmmm...

Cyrille

Shane Mingins

unread,
Feb 15, 2007, 12:00:34 PM2/15/07
to WellR...@googlegroups.com
Hi Cyrille

I did this once ... I wanted another specific fixture for a test. I
did something like this:

def setup
User.delete_all
filename = './test/fixtures/users_fixture_for_test.yml'
YAML.load_file(filename).each {|key, value|
User.create(value)
}
end

HTH
Shane


--
Shane Mingins

Cyrille

unread,
Feb 15, 2007, 3:10:52 PM2/15/07
to WellRailed
Hi Shane,

good idea! A hack, but less scary than patching Rails...

Thanks for that.

Cyrille

On Feb 16, 6:00 am, "Shane Mingins" <shane.ming...@gmail.com> wrote:
> Hi Cyrille
>
> I did this once ... I wanted another specific fixture for a test. I
> did something like this:
>
> def setup
> User.delete_all
> filename = './test/fixtures/users_fixture_for_test.yml'
> YAML.load_file(filename).each {|key, value|
> User.create(value)
> }
> end
>
> HTH
> Shane
>

Henry Maddocks

unread,
Feb 15, 2007, 4:40:25 PM2/15/07
to WellR...@googlegroups.com
Don't understand why you need more than one file. Could you post your yaml file?
Reply all
Reply to author
Forward
0 new messages