Fixtures Without Rails
So continuing my series of ActiveRecord Without Rails, I thought I would inspect another popular feature of ActiveRecord, Fixtures.
Fixtures are a great way to populate your database with an expected result set. It makes it easy to prepare your database for testing and presentation to your client.
Why would you do this? Well you’ve already got ActiveRecord without Rails working, and probably are using the Migrations without Rails. So why not continue with fixtures?
Note:
- I won’t go into getting ActiveRecord working without Rails, please refer to that post. It’s dead simple.
- All these commands are executed from the root directory of your non-rails application.
Creating the Fixtures Directory
The fixtures will need to be stored somewhere, so we’ll just create the necessary directory for them to enjoy a rather peaceful life.
mkdir -p test/fixtures
Setting up the Rakefile
Now we’ll need a Rakefile. If your not familiar with Rakefiles just create a file called: Rakefile 
Note: :
- If you were following my ActiveRecord Migrations Without Rails, this is the same
Rakefile. I’ve changed the default task to execute to load the fixtures instead. You can opt to continue using themigratetask if you want. Just remember to call eitherrake migrateorrake fixtures, depending on which task you would like call, that isn’t your default task. - If you were not following my ActiveRecord Migrations Without Rails, you can ignore the migrate task.
Your First Fixture
Now we’ll create our first fixture, and save it into test/fixtures/users.yml
. The filename corresponds to the table for where the fixtures will be loaded into.
aizatto:
name: Ezwan Aizat Bin Abdullah Faiz
Warning:
Now the fixture is ready for loading and we execute:
rake
There isn’t much output at all (if you even want to call that output) but you’ll see something like:
(in /path/to/app)
Loading Specific Fixtures
By default all fixtures are loaded into the database. Rather than loading all the fixtures, you can also specify which fixtures you want to load by appending them to a FIXTURES argument.
rake FIXTURES=users,roles
Embedded Ruby in Fixtures
In the same way like Rails, you can also continue to embed ruby inside your fixtures, giving you more flexibility to your hour models will be loaded.
aizatto:
name: Ezwan Aizat Bin Abdullah Faiz
created_at: <%= 1.day.ago %>
Conclusion
So by now you’ve probably accumulated quite a bit of knowledge in using ActiveRecord and its features outside of Rails.
So lets hear it, what else would you like to see?



Chris said,
June 4, 2007 @ 9:19 am
Hi,
Do you know how to do this without Rake inside a unit test? So you can just put something like:
fixtures :table1, :table2 at the top of a unit test?
That seems to be a bit trickier.
There’s an old post on ruby forum from a year ago, where this guy has a custom test helper.
http://opensvn.csie.org/ezra/rails/ez_where_two/test/test_helper.rb
Then after you use that test helper, supposedly you can use fixtures in your own unit tests outside of rails. I copied the test helper but I still cant get it to work. Putting fixtures :table1, :table2 does absolutely nothing…
I wonder if the example is just too old. I can get them to work if I do create_fixtures(table1, table2) inside a specific test method, but its not as nice as just putting fixtures :table1, etc. at the top of the entire unit test class.
-Chris
Jevin said,
February 25, 2008 @ 11:36 pm
As a response to Chris. The Rake::TestTask works great for me:
Rake::TestTask.new do |t|
t.libs << “test”
t.test_files = FileList[’test/*Test.rb’]
t.verbose = true
end
Look up the Rake::TestTask for more info.