[rspec-users] Using rspec_on_rails without a test

Scott Taylor mailing_lists at railsnewbie.com
Fri Feb 23 15:53:05 EST 2007


Looks like you can run rspec w/out hitting the database by doing the  
following:

1.  Setting up a new rake task which does not have db:test:prepare as  
a dependency:

namespace :spec do
   desc "Run the specs under spec/models without a database"
   Spec::Rake::SpecTask.new(:models_no_db) do |t|
     t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
     t.spec_files = FileList['spec/models/**/*_spec.rb']
   end
end

2.  Calling an error on access the database with a new helper:

ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/ 
environment")
require 'spec/rails'

# Even if you're using RSpec, RSpec on Rails is reusing some of the
# Rails-specific extensions for fixtures and stubbed requests, response
# and other things (via RSpec's inherit mechanism). These extensions are
# tightly coupled to Test::Unit in Rails, which is why you're seeing  
it here.

module Spec
   module Rails
     class ActiveRecord::Base
       def self.connection
         raise InvalidActionError, "You cannot access the database",  
caller
       end
     end
   end
end

class InvalidActionError < StandardError
end


The test database must still be set up in config/database.yml, and I  
believe it must also exist

Should I also post this to the rspec rubyforge site?

Best,

Scott





More information about the rspec-users mailing list