Rspec Without db:test:prepare
By Phil Matarese on April 23rd, 2008
First Things First
Once upon a time, after deploying an Rspec'd app for the first time, we wanted to run all the specs on our staging server to see that everything was running as expected. Only, there was a problem with dependencies.
At the time we had a pretty gross, gem-dependency soup cooking up in our app. We had rspec as a frozen gem and rspec_on_rails as a plugin, and this was causing (unconfirmed) load-order problems for rake. After checking the rspec documentation again, we changed rspec from a gem to a plugin, which straightened everything out (I think).
The Basics
Basically, our current setup is to have piston (essential for plugin management), rcov (keep the coverage above 50% or the bus blows up), and ZenTest (because autotest is fun) installed locally as gems on all development machines - there is no reason for us to use these on our production or staging servers. Within the app we have rspec_on_rails, rspec_autotest, and rails_rcov (which uses magic) installed in the plugins directory (under the watchful eye of piston.) These are part of the app, not only because the rspec docs like it that way, but because we'd like to ensure all the tests pass after a deployment. Everything is all nice and neat now. Shouldn't have anymore problems. Nope. No sir. No way. Uh-uh.
The Problem
When running rake spec, the database can't be found. But... we installed it... right? Of course, it's right there. And... the permissions? They're good, too. Uhh... Hey, did that say the "development" database can't be found? What the...
Unbeknownst to us, the spec task had as a prerequisite db:test:prepare, which takes a schema_dump from the development database and uses it to create a test database. This is something we don't need. Unfortunately, there's no option or flag that you can use to disable this prerequisite.
The Solution
So this one got a little hairy. The load order for rake tasks is rails, custom code in lib/tasks/*, and then rspec. So, I couldn't drop the spec prerequisites with my custom code, because they didn't exist yet. My first attempt to solve this involved wiping out db:test:prepare completely, no matter what. I was a bit hesitant to do this, because it had nothing to do with what I was really trying to accomplish – I've got nothing against db:test:prepare, I just want it decoupled from rspec. Here is our current solution which only wipes out db:test:prepare when the spec task is invoked. Not only does this solve the problem of testing on a staging server, but it also speeds up testing in our local development environments. Enjoy.
1 # bock_db_test_prepare.rb
2
3 # without this, you could never
4 # remove elements of a task
5 class Rake::Task
6 def clear_it
7 @actions.clear
8 @prerequisites.clear
9 end
10 end
11
12 # set up the blocking as a rake task,
13 # so we can add it as a prereq to spec
14 namespace :ap do
15 namespace :spec do
16 task :skip_db_test_prepare do
17 Rake::Task['db:test:prepare'].clear_it
18 end
19 end
20 end
21
22 # block it!
23 task :spec => 'ap:spec:skip_db_test_prepare' The final line works even though the spec task doesn't exist yet. Essentially, our custom code creates an empty spec task with our single prerequisite. When rspec loads, the real task and prerequisites will be appended after ours. Success.

Unlike other publications, I don't read
That script is based on 
And, since I was looking for a reason to try out 