[Chirb] tools, already in play and coming soon

Paul Gross pgross at gmail.com
Tue Jun 10 01:02:44 EDT 2008


> Lastly, I may have mis-spoke about rails loading up columns from 
> schema.rb - I'm looking into it and will follow up when I learn the 
> truth. So Ryan - keep using unit-record for now :)
>

UnitRecord (http://unit-test-ar.rubyforge.org/) parses schema.rb to 
determine the columns and overrides the connection method on 
ActiveRecord::Base, so there will be no database hits.  The philosophy 
is that unit tests should be entirely isolated, including no database 
access.

The meat of stub_model is:

def stub_model(model_class, stubs = {})
  returning model_class.new do |model|
    (class << model; self; end).class_eval do
      def connection
        raise Spec::Rails::IllegalDataAccessException.new("stubbed 
models are not allowed to access the database")
      end
    end
  end
end

It defines connection on the singleton class to raise an exception.  
However, as soon as you do model_class.new, rails goes to the database 
to fetch the columns.  So, stub_model will still cause a database hit 
once for every object, but not again.  The philosophy here seems to be 
that a few database hits are ok for ease of use.

Thanks,

Paul Gross
http://www.pgrs.net




More information about the ChicagoGroup-Members-List mailing list