Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

mocking ActiveRecord (See related posts)

A search didn't turn anything up, so I tried this:

   1  ActiveRecord::Base.class_eval do
   2    alias_method :save, :valid?
   3    def self.columns() @columns ||= []; end
   4    
   5    def self.column(name, sql_type = nil, default = nil, null = true)
   6      columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type, null)
   7    end
   8  end


The mock model:

   1  class User < ActiveRecord::Base
   2    validates_presence_of :login
   3    column :id,       :integer
   4    column :login,    :string
   5    column :password, :string
   6    column :active,   :boolean, true
   7  end


This should provide a quick way to test validations and things like that...

Comments on this post

floehopper posts on Aug 26, 2006 at 14:33
Very neat.

Have you had a look at Mocha which allows you to mock/stub methods on concrete objects and classes. So you can mock the ActiveRecord methods like create, save, destroy.

You need to create an account or log in to post comments to this site.


Click here to browse all 5827 code snippets

Related Posts