Class: Fixtures

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/fixtures.rb

Overview

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

YAML fixtures

Additionally, fixtures supports yaml files. Like fixture files, these yaml files have a pre-defined format. The document must be formatted like this:

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

In that file, there’s two records. Each record must have two parts: ‘name’ and ‘data’. The data that you add must be indented like you see above.

Yaml fixtures file names must end with .yaml as in people.yaml or camel.yaml. The yaml fixtures are placed in the same directory as the normal fixtures and can happy co-exist. :)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, table_name, fixture_path, file_filter = /^\.|CVS|\.yaml/) ⇒ Fixtures

Returns a new instance of Fixtures.



72
73
74
75
76
77
78
# File 'lib/active_record/fixtures.rb', line 72

def initialize(connection, table_name, fixture_path, file_filter = /^\.|CVS|\.yaml/)
  @connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter
  @fixtures = read_fixtures

  delete_existing_fixtures
  insert_fixtures
end

Class Method Details

.create_fixtures(fixtures_directory, *table_names) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_record/fixtures.rb', line 59

def self.create_fixtures(fixtures_directory, *table_names)
  connection = block_given? ? yield : ActiveRecord::Base.connection
  ActiveRecord::Base.logger.level = Logger::ERROR

  fixtures = [ table_names ].flatten.collect do |table_name|
    Fixtures.new(connection, table_name, "#{fixtures_directory}/#{table_name}")
  end

  ActiveRecord::Base.logger.level = Logger::DEBUG

  return fixtures.size > 1 ? fixtures : fixtures.first
end

Instance Method Details

#[](key) ⇒ Object

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



81
82
83
# File 'lib/active_record/fixtures.rb', line 81

def [](key)
  @fixtures[key]
end

#lengthObject

Get the number of fixtures kept in this container



86
87
88
# File 'lib/active_record/fixtures.rb', line 86

def length
  @fixtures.length
end