Mr eel
Fixtures with Datamapper and Merb
If you’re to be using Datamapper with Merb, you might be wondering how to get your test fixtures into your test database.
Thankfully it’s pretty simple. For those of you using Rspec, just stick this bit of code in you spec/spec_helper.rb
# Make sure the schema is in sync
DataMapper::Base.auto_migrate!
def fixtures(*files)
files.each do |name|
klass = Kernel::const_get(Inflector.classify(Inflector.singularize(name)))
entries = YAML::load_file(File.dirname(__FILE__) + "/fixtures/#{name}.yaml")
entries.each do |name, entry|
klass::create(entry)
end
end
end
If you haven’t seen the auto_migrate! in DataMapper before, be aware that it destroys and recreates your database schema. I like to add it to my spec_helper so that the test DB is in sync with development, without me having to run a rake task.
This method assumes your fixtures are in the spec/fixtures directory and use the yaml extension. If you’re using .yml instead, you’ll need to change the third line in the fixtures method.
Now just call fixture inside your describe blocks.
describe "make sure the yowie is real" do
fixtures :yowies
end
Keep in mind this will likely be a bit slow if you have loads and loads of fixtures, but for simple cases it works nice!
Comments