Skip to content

Instantly share code, notes, and snippets.

@awendt
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awendt/9842536 to your computer and use it in GitHub Desktop.
Save awendt/9842536 to your computer and use it in GitHub Desktop.
Answer for SO question 22569738
gem 'activerecord', '4.0.2'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :users do |t|
t.string :username
end
create_table :accounts do |t|
t.integer :user_id
t.string :name
end
end
class User < ActiveRecord::Base
has_one :account, dependent: :destroy, autosave: true
validates :username, presence: true, uniqueness: true
end
class Account < ActiveRecord::Base
belongs_to :user
validates_presence_of :name
end
class BugTest < Minitest::Test
def test_basic_assumptions
refute User.new.valid?
assert User.new(username: 'bob').valid?
refute Account.new.valid?
assert Account.new(name: 'alice inc.').valid?
end
def test_both_are_valid
users_before, accounts_before = User.count, Account.count
user = User.new(username: 'tom')
account = user.create_account(name: 'jerry inc.')
user.save && account.save
assert_equal users_before+1, User.count
assert_equal accounts_before+1, Account.count
end
def test_invalid_user_valid_account
users_before, accounts_before = User.count, Account.count
invalid_user = User.new
account = invalid_user.create_account(name: 'batman')
invalid_user.save && account.save
assert_equal users_before, User.count
assert_equal accounts_before, Account.count
end
def test_valid_user_invalid_account
users_before, accounts_before = User.count, Account.count
user = User.new(username: 'robin')
invalid_account = user.create_account
user.save && invalid_account.save
assert_equal users_before, User.count
assert_equal accounts_before, Account.count
end
end
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.0.2'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :users do |t|
t.string :username
end
create_table :accounts do |t|
t.integer :user_id
t.string :name
end
end
class User < ActiveRecord::Base
has_one :account, dependent: :destroy, autosave: true
validates :username, presence: true, uniqueness: true
end
class Account < ActiveRecord::Base
belongs_to :user
validates_presence_of :name
end
class BugTest < Minitest::Test
def test_basic_assumptions
refute User.new.valid?
assert User.new(username: 'bob').valid?
refute Account.new.valid?
assert Account.new(name: 'alice inc.').valid?
end
def test_both_are_valid
users_before, accounts_before = User.count, Account.count
user = User.new(username: 'tom')
account = user.build_account(name: 'jerry inc.')
user.save
assert_equal users_before+1, User.count
assert_equal accounts_before+1, Account.count
end
def test_invalid_user_valid_account
users_before, accounts_before = User.count, Account.count
invalid_user = User.new
account = invalid_user.build_account(name: 'batman')
invalid_user.save
assert_equal users_before, User.count
assert_equal accounts_before, Account.count
end
def test_valid_user_invalid_account
users_before, accounts_before = User.count, Account.count
user = User.new(username: 'robin')
invalid_account = user.build_account
user.save
assert_equal users_before, User.count
assert_equal accounts_before, Account.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment