=== About ===
Fixtures is Merb plugin which provides fixture system for Merb.
1) Fixtures are written in Ruby, not in YAML, so it's more powerful than YAML one. For example it's much easier to add HABTM relationships.
2) Fixtures aren't just for testing, but they are useful for development and even for migrations
=== Setup ===
git clone git://github.com/botanicus/merb-fixtures.git
cd merb-fixtures
rake install
# config/init.rb
dependency 'merb-fixtures'
By default, fixtures folder is app/fixtures. If you like to puts your fixtures on other place, feel free to change it in your init.rb:
# config/init.rb
Merb::Plugins.config[:fixtures] = {
:directory => Merb.root / "app" / "fixtures"
}
=== Writing fixtures ===
First you must write a fixture:
# app/fixtures/author.rb
fixture_for(Author, :botanicus) do
self.first_name = "Jakub"
self.last_name = "Šťastný"
self.nickname = "Botanicus"
self.homepage = "http://botablog.cz"
self.posts = [ ... ]
end
=== Usage from merb console or specs ===
Now you can use it - from your specs or just from merb console:
# Get fixture named :botanicus
Author.fixture(:botanicus)
=> #<Author last_name="Šťastný" homepage="http://botablog.cz" first_name="Jakub" nickname="Botanicus">
# Get all the author's fixtures
Author.fixtures
=> [#<Author last_name="Šťastný" homepage="http://botablog.cz" first_name="Jakub" nickname="Botanicus">]
=== Usage in migrations ===
# Usage in migrations
migration 1, "Add default author" do
up do
Author.fixture(:botanicus).save
end
down do
# foo
end
end
=== FAQ ===
== HABTM ==
# TODO: describe different types of associations
class ModelOne
has 0..n, :model_two
end
class ModelTwo
belongs_to :model_one
# model_one_id
end
fixture_for(ModelOne, :one) do
# foo bar
end
require_fixtures :model_one
fixture_for(ModelTwo, :two) do
self.model_one = ModelOne.fixture(:model_one)
end
1) save ModelOne