Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A simple migration-like library for doing models and tables in tests
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time


ReversibleData makes it easy to define models / tables that are automatically brought up and down as part of the testing process. It doesn't use migrations instead simply creating and dropping tables.


Is simple, simply add:

gem 'reversible_data', '~> 1.0'

To your Gemfile and run bundle install.

Inside your test setup, use the below examples on how to declare tables. Finally, also check for our example Shoulda and Rspec examples.

Creating and Managing Models and Tables with ReversibleData::Table

To get started, you need to setup and require the gem and establish a connection. For most plugins, the example should fill most uses.

require 'reversible_data'


table = ReversibleData.add(:users) do |t|
  t.string  :name
  t.integer :age

# Optional: a class_eval block

table.define_model do
  validates_presence_of :name

# Reset the definition

# You can then use table.create_table, table.drop_table,
# table.create_model, table.remove_model.

Using ReversibleData.add, you can specify a :class_name option

ReversibleData.add(:awesome_sauce, :class_name => "Ninja")

If nil, it will use the rails default method of generating a model name.

Finally, it accepts an options hash with :skip_model and :skip_table which make it easy to skip creating part.


The table manager is a simple wrapper around multiple tables which makes it super easy to do the up / down process. E.g.:

manager = ReversibleData.manager_for(:users, :awesome_sauce)

Test Suite Integration

RSpec2 Macro

To make it easy to integrate with RSpec2-based test suites (e.g. Rocket Pants uses it in it's integration tests), we provide helpers:

In your spec_helper.rb, First add:

config.extend ReversibleData::RSpec2Macros

Inside your RSpec.configure block. Next, In your tests:

describe "Something" do

  context 'with a model' do
    use_reversible_tables :user

    it 'should be awesome' do
      User.count.should == 42 # Will always fail.



Finally, note the use_reversible_tables method accepts options, the only current one being :scope, which lets you specify the setup / teardown scope, defaulting to :each but changeable to :all (e.g. in RocketPants).

Shoulda Macro

Conveniently, there is a simple Shoulda macro you can use to make make writing these tests easy. It's as simple as:

class MyTest < Test::Unit::TestCase
  extend ReversibleData::ShouldaMacros

  with_tables :users, :awesome_sauce do

    # Your tests here...


Something went wrong with that request. Please try again.