Testing your ActiveRecord migrations
Sometimes you have a data migration that creates or modifies records in some way, and you would like to test it in your unit tests right? Except that Rails typically just clones the structure of your dev database, not its data. So how do you test that a migration succeeded? Simple, just invoke it during your test:
This code is written using the shoulda plugin which gives a slightly more verbose breakdown of the setup and the test, but it can be done with test/unit just as well.
context "orphan user migration" do
setup do
require "#{RAILS_ROOT}/db/migrate/163_create_orphan_owner"
CreateOrphanOwner.up
end
should "create the orphan user" do
assert orphan = User.orphan_owner
assert orphan.role?(User::ORPHAN_OWNER)
end
end










1 Comment