Prev | Current Page 137 | Next

Brad Ediger

"Advanced Rails"


The first piece of the puzzle is the database.yml file, which includes not only configuration
blocks for standard DBMSs, but also for SQLite and SQLite3, which save their
database in a local file:
sqlite:
:adapter: sqlite
:dbfile: state_machine.sqlite.db
sqlite3:
:adapter: sqlite3
:dbfile: state_machine.sqlite3.db
# (postgresql and mysql elided)
The schema files, fixtures, and models are self-explanatory; they are a Ruby schema
file, YAML fixtures, and ActiveRecord model classes, respectively. The real magic
happens in test_helper.rb, which ties everything together.
The test helper first sets up Rails load paths and loads ActiveRecord. Then it loads
database.yml and instructs ActiveRecord to connect to the database (defaulting to
SQLite):
config = YAML::load(IO.read(File.dirname(_ _FILE_ _) + '/database.yml'))
ActiveRecord::Base.logger = Logger.new(File.dirname(_ _FILE_ _) + "/debug.log")
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
Next, the schema file is loaded into the database:
load(File.dirname(_ _FILE_ _) + "/schema.rb") if
File.exist?(File.dirname(_ _FILE_ _) + "/schema.rb")
Finally, the plugin??™s fixture path is set as TestCase??™s fixture path and added to the
load path so that models in that directory will be recognized:
Test::Unit::TestCase.fixture_path = File.dirname(_ _FILE_ _) + "/fixtures/"
$LOAD_PATH.unshift(Test::Unit::TestCase.


Pages:
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149