Don't Forget to Plant It!

Problems With Rails Fixture Labels?

Newer versions of Rails has a nice feature where you can use label references for fixtures. So instead of:

1
2
3
4
# posts.yml
test_post:
user_id: 1
title: My Test Post

You can do this:

1
2
3
test_post:
user: quentin
title: My Test Post

However, if your model class name is in a pluralized form, you might find that label references won’t work. That’s because fixtures derive their class name from the singular form of the table name by default. Fortunately, you can fix this by adding this line to your TestHelper:

1
2
3
4
5
class Test::Unit::TestCase
# Explicitly map the table name to class name
set_fixture_class :accounts => 'accounts'
end

Hopefully, this will save someone else from having to dig through the Rails fixtures internals.

Comments