Skip to content

Instantly share code, notes, and snippets.

@ssoroka
Created March 25, 2013 20:26
Show Gist options
  • Save ssoroka/5240395 to your computer and use it in GitHub Desktop.
Save ssoroka/5240395 to your computer and use it in GitHub Desktop.
Make your data layer an implementation detail. :)
require 'test_helper'
class ARFactory
def self.new(table_name)
Class.new(ActiveRecord::Base) do
self.table_name = table_name
end
end
end
class User # look mom, no < ActiveRecord::Base! yay proper encapsulation
@@datastore = ARFactory.new('test_users')
def initialize
@user = @@datastore.new
end
def name=(val)
@user.name = val
end
def save
@user.save
end
def self.find_by_name(string)
@@datastore.find_by_name(string)
end
end
class ARFactoryTest < ActiveSupport::TestCase
def setup
ActiveRecord::Migration.create_table('test_users') do |t|
t.column 'name', :string
end
@user = User.new
end
def teardown
ActiveRecord::Migration.drop_table('test_users')
end
def test_can_save_and_fetch_user
@user.name = 'steve'
@user.save
@user = User.find_by_name('steve')
assert_equal 'steve', @user.name
end
def test_test_user_not_defined
assert !defined?(TestUser)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment