Fixtures (Class)

In: vendor/activerecord/lib/active_record/fixtures.rb
Parent: Object

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

Methods

[]   length   new  

Public Class methods

[Source]

    # File vendor/activerecord/lib/active_record/fixtures.rb, line 32
32:   def initialize(connection, table_name, fixture_path, file_filter = /^\.|CVS/)
33:     @connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter
34:     @fixtures = read_fixtures
35: 
36:     delete_existing_fixtures
37:     insert_fixtures
38:   end

Public Instance methods

Access a fixture hash by using its file name as the key

[Source]

    # File vendor/activerecord/lib/active_record/fixtures.rb, line 41
41:   def [](key)
42:     @fixtures[key]
43:   end

Get the number of fixtures kept in this container

[Source]

    # File vendor/activerecord/lib/active_record/fixtures.rb, line 46
46:   def length
47:     @fixtures.length
48:   end

[Validate]