Skip to content

Instantly share code, notes, and snippets.

@oseiskar
Created June 23, 2015 09:19
Show Gist options
  • Save oseiskar/38e09756902fbec211d0 to your computer and use it in GitHub Desktop.
Save oseiskar/38e09756902fbec211d0 to your computer and use it in GitHub Desktop.
ACTIVE_RECORD_VERSION =
'master' # fails
# '4.2.2' # fails
# '4.1.11' # fails
# '4.0.13' # passes
if ACTIVE_RECORD_VERSION == 'master'
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
end
else
gem 'activerecord', ACTIVE_RECORD_VERSION
end
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 :posts, force: true do |t|
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
validates :post, presence: true
end
class BugTest < Minitest::Test
def setup
@post = Post.create!
end
# This test fails with newer versions of ActiveRecord
def test_must_have_thing_id_when_created_from_association
comment = @post.comments.create!
comment.post_id = nil
assert comment.invalid?
end
def test_must_have_thing_when_created_with_association
comment = @post.comments.create!
comment.post = nil
assert comment.invalid?
end
def test_must_have_thing_id_when_created_with_thing_id
comment = Comment.create!(post_id: @post.id)
comment.post_id = nil
assert comment.invalid?
end
def test_must_have_thing_id_when_created_with_thing
comment = Comment.create!(post: @post)
comment.post_id = nil
assert comment.invalid?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment