1.1. Fixtures

Yaml fixtures have a new format.

Before:

    name: david
    data:
     id: 1
     name: David Heinemeier Hansson
     birthday: 1979-10-15
     profession: Systems development
    ---
    name: steve
    data:
     id: 2
     name: Steve Ross Kellock
     birthday: 1974-09-27
     profession: guy with keyboard

After:


    david:
     id: 1
     name: David Heinemeier Hansson
     birthday: 1979-10-15
     profession: Systems development

    steve:
     id: 2
     name: Steve Ross Kellock
     birthday: 1974-09-27
     profession: guy with keyboard

Along with the new YAML format comes better error handling. Fixtures are now checked for duplicated key (field/attribute) names.

YAML fixtures are now the preferred kind. The generators have been updated to create YAML fixtures.

Fixtures have been promoted up the directory chain by 1 level. They are now placed in test/fixtures/. A separate sub-directory for each model is no longer required.

Fixtures also end with .yml instead of .yaml to be more consistent.

An fixtures directory might look something like this:
    test/fixtures/chocolate_bars.yml
    test/fixtures/chips.yml
    test/fixtures/colas.yml
    test/fixtures/denture_types.yml

Another fixture format has been introduced: comma-separated values. CSV is a great format for data exported from databases and spreadsheets as there is no need to write an extra script to convert the data to YAML.

The CSV fixtures live in the same directory as the other fixtures but end with the .csv file extension, as in:
    test/fixtures/cannibal_boxers.csv
    test/fixtures/tv_evangelists.csv
    test/fixtures/celebrity_lawyers.csv
    test/fixtures/federal_penitentiaries.csv

CSV fixtures are divided into two parts: the header and everything else. The header is placed on the first line of the file and is a comma-separated list of field names. Each following line thereafter represents a record. For example:

    id, name, real_profession, affiliation
    1, Barney, Hitman, F.B.I
    2, "Sharon, Lois, and Braham", Mobsters, Soprano Family
    3, Elvis, Shoe Salesman, Cincinatti Walmart
    4, Michael ""Air"" Jordan, Wealthy Tycoon, FamousGuysWithPerfumes.com

In unit tests, you now have the option to include multiple fixtures with one line of code.

    class SillyPuttyTest < Test::Unit::TestCase
      fixtures :toxic_substances, :vending_machines, :mind_altering_food
    end

With the fixtures method placed in a TestCase, it has the effect of loading each specified fixture file at test startup and deletion on each test teardown. The create_fixtures fixtures method is still available for data which has no corresponding model (ie. join tables).

In addition to fixtures being available as a Hash instance variable, they are also available as their native model type via a new find method. In the example above:
    @toxic_substances["trimethylchloride"].find
This produces an object of type ToxicSubstance. This allows you to get at the methods for a particular class for all your fixtures instead of having to load them through the database.