Over a million developers have joined DZone.

Mocking ActiveRecord

DZone's Guide to

Mocking ActiveRecord

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

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

The mock model:

class User < ActiveRecord::Base
  validates_presence_of :login
  column :id,       :integer
  column :login,    :string
  column :password, :string
  column :active,   :boolean, true

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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}