Skip to content

Instantly share code, notes, and snippets.

@benjaminwood
Created February 10, 2019 22:34
Show Gist options
  • Save benjaminwood/0f6a12495167244cc3add68855d4db3e to your computer and use it in GitHub Desktop.
Save benjaminwood/0f6a12495167244cc3add68855d4db3e to your computer and use it in GitHub Desktop.
Test case reproducing an issue that causes a record to be inserted without a require foreign key (foo_id)
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", "5.2.2"
gem "sqlite3", '~> 1.3.13'
gem "factory_bot", "4.11.1"
end
require "active_record"
require "minitest/autorun"
require "logger"
require "factory_bot"
# 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, force: true do |t|
end
create_table :tasks, force: true do |t|
t.integer :user_id, null: false, index: true
t.integer :foo_id, null: false, index: true
end
end
class User < ActiveRecord::Base
has_one :task
end
class Task < ActiveRecord::Base
belongs_to :user, dependent: :destroy
belongs_to :foo, class_name: 'User'
end
FactoryBot.define do
factory :user do
end
factory :task do
association :user, strategy: :build
association :foo, factory: :user, strategy: :build
end
end
class BugTest < Minitest::Test
def test_factory_bot_stuff
task = FactoryBot.create(:task)
assert task.foo_id.present?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment