Skip to content

Instantly share code, notes, and snippets.

@charlie
Created March 12, 2011 01:54
Show Gist options
  • Save charlie/866948 to your computer and use it in GitHub Desktop.
Save charlie/866948 to your computer and use it in GitHub Desktop.
require 'rubygems'
require "test/unit"
require "active_record"
require "sqlite3"
module Test::Unit
class TestCase
def setup
$VERBOSE = nil
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "./.test.sqlite3")
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "test.log"))
ActiveRecord::Base.silence do
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define :version => 0 do
create_table "users", :force => true do |t|
t.integer "id"
end
create_table "toys", :force => true do |t|
t.integer "id"
t.integer "user_id"
t.text "name"
t.text "model"
end
end
end
end
end
end
class User < ActiveRecord::Base
has_many :toys
end
class Toy < ActiveRecord::Base
belongs_to :user
attr_accessible :name, :model
end
class TheTest < Test::Unit::TestCase
def test_find_or_create_by_this_and_that
u = User.create
software_engineer_barbie = u.toys.create(:name => "Barbie", :model => "Software Engineer")
result = u.toys.find_or_create_by_name_and_model("Barbie", "Software Engineer")
assert result.id == software_engineer_barbie.id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment