Created
March 20, 2020 19:51
-
-
Save entcheva/22b7ee4181654d5e35036d9bb31c6eb3 to your computer and use it in GitHub Desktop.
Reproduction script for Issue #1372
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
gem "factory_bot", "~> 5.0" | |
gem "activerecord" | |
gem "sqlite3" | |
end | |
require "active_record" | |
require "factory_bot" | |
require "minitest/autorun" | |
require "logger" | |
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| | |
t.string :title | |
end | |
create_table :comments, force: true do |t| | |
t.references :post | |
end | |
end | |
class ApplicationRecord < ActiveRecord::Base | |
self.abstract_class = true | |
end | |
class Post < ApplicationRecord | |
has_many :comments, dependent: :destroy | |
end | |
class Comment < ApplicationRecord | |
belongs_to :post | |
end | |
FactoryBot.define do | |
factory :post do | |
after(:build) do |post| | |
p post.comments | |
end | |
title { "xxx" } | |
end | |
factory :comment do | |
end | |
end | |
class FactoryBotTest < Minitest::Test | |
def test_comments_destroy_factorybot | |
Comment.destroy_all | |
post = FactoryBot.create(:post) | |
3.times { FactoryBot.create(:comment, post: post) } | |
assert_equal 3, Comment.count | |
post.destroy! | |
assert_equal 0, Comment.count | |
end | |
def test_comments_destroy_factorybot_2 | |
Comment.destroy_all | |
post = Post.new | |
p post.comments # This test passes if you comment this line out | |
post.save! | |
3.times do | |
comment = Comment.new | |
comment.post = post | |
comment.save! | |
end | |
assert_equal 3, Comment.count | |
post.destroy! | |
assert_equal 0, Comment.count | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment