| Path: | vendor/activerecord/lib/active_record/fixtures.rb |
| Last Update: | Fri Aug 06 14:53:13 CEST 2004 |
Fixtures are a way of organizing data that you want to test against. Each fixture file is created as a row in the database and created as a hash with column names as keys and data as values. All of these fixture hashes are kept in an overall hash where they can be accessed by their file name.
Example:
Directory with the fixture files
developers/
david
luke
jamis
The file david then contains:
id => 1 name => David Heinemeier Hansson birthday => 1979-10-15 profession => Systems development
Now when we call @developers = Fixtures.new(ActiveRecord::Base.connection, "developers", "developers/") all three developers will get inserted into the "developers" table through the active Active Record connection (that must be setup before-hand). And we can now query the fixture data through the @developers hash, so @developers["david"]["name"] will return "David Heinemeier Hansson" and @developers["david"]["birthday"] will return Date.new(1979, 10, 15).
This can then be used for comparison in a unit test. Something like:
def test_find
assert_equal @developers["david"]["name"], Developer.find(@developers["david"]["id"]).name
end