Fixie
A standalone library for managing test fixture data with good support for multiple databases.
Installation
Add this line to your application's Gemfile:
gem 'fixie'
And then execute:
$ bundle
Or install it yourself as:
$ gem install fixie
Usage
To use Fixie, you first create some fixture files in a directory called fixtures somewhere in your load path, typically in the test directory:
test/
└── fixtures
└── default
├── cities.yml
└── countries.yml
You must put all your fixtures into a subdirectory of the fixtures directory. The name of the directory should be a good logical name for the database that the fixtures. If you have only one database, you should use default as the name, but if you have an app with customers in one database and orders in another, you would name them customers and orders.
Fixie uses Sequel to load the data into the database. Fixie will work even if you aren't using Sequel in your application. You must configure the Fixie databases and then call load_fixtures to get the fixtures to actually be loaded. Your test helper might look like this:
Fixie.dbs[:default] = Sequel.sqlite
Fixie.load_fixturesNow all the fixtures will be loaded into the default database. In order to access them from a test, you can the fixture as a Hash like this:
Fixie::Fixtures.countries(:us)You can also include the Fixie::Fixtures model in your tests and then just call the method directly:
include Fixie::Fixtures
def test_something
assert_equal "US", countries(:us)
endIf you have models in your application and you want to get instances of the model back instead of Hashes, you need to mix the Fixie::Model module into the base class of your models. For example, say you have models defined like this:
class Model
end
class Country < Model
end
class City < Model
endIn your test helper, you mix in Fixie::Model like this:
Model.extend Fixie::ModelNow in your tests, you can refer to fixtures like this:
def test_something
assert_equal "US", Country.fixture(:us)
endFixtures are defined in YAML files like this:
us:
id: 1
name: United StatesIf left out, the value for the id attribute will be automatically generated based on the name of the fixture:
us:
name: United StatesYou can then use it in other fixtures to reference the other record by name instead of id:
baltimore:
name: Baltimore
country: usYou can also use ERB in the YAML files:
baltimore:
name: Baltimore
country: us
created_at: <%= Time.now %>The ERB is evaluated in the context of the Fixie module, so if there is anything else you want to make available in that context, just mix the module into Fixie:
Fixie.extend FastGettext::Translationbaltimore:
name: <%=_ "Baltimore" %>
country: us
created_at: <%= Time.now %>Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request