Skip to content

Instantly share code, notes, and snippets.

@betesh
Last active August 29, 2015 14:25
Show Gist options
  • Save betesh/209e8c6be27c709a0a8d to your computer and use it in GitHub Desktop.
Save betesh/209e8c6be27c709a0a8d to your computer and use it in GitHub Desktop.
failing-test-rails-issue-20882
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# 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
# Note that if you use autosave, both tests pass:
# has_many :comments, autosave: true
end
class Comment < ActiveRecord::Base
belongs_to :post
attr_accessor :string
validates_presence_of :string
end
# Since the comment is marked for destruction, saving the post should not save the comment
class BugTest < Minitest::Test
def post
@post ||= Post.new
end
def when_i_build_a_valid_comment
post.comments.build(string: "ABC").mark_for_destruction
end
def when_i_build_an_invalid_comment
post.comments.build.mark_for_destruction
end
def expect_the_comment_not_to_be_persisted
assert_equal 0, Comment.count
end
def test_valid_comment
when_i_build_a_valid_comment
post.save!
expect_the_comment_not_to_be_persisted
end
def test_invalid_comment
when_i_build_an_invalid_comment
post.save!
expect_the_comment_not_to_be_persisted
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment