diff --git a/lib/factory_bot/linter.rb b/lib/factory_bot/linter.rb index a381d4493..f0bb9ae27 100644 --- a/lib/factory_bot/linter.rb +++ b/lib/factory_bot/linter.rb @@ -109,7 +109,7 @@ def error_message_type def in_transaction if defined?(ActiveRecord::Base) - ActiveRecord::Base.transaction do + ActiveRecord::Base.transaction(joinable: false) do yield raise ActiveRecord::Rollback end diff --git a/spec/acceptance/lint_spec.rb b/spec/acceptance/lint_spec.rb index 28930502c..5f40b35c6 100644 --- a/spec/acceptance/lint_spec.rb +++ b/spec/acceptance/lint_spec.rb @@ -44,6 +44,23 @@ expect { FactoryBot.lint }.to_not raise_error end + it "runs after_commit callbacks when linting in a ActiveRecord::Base transaction" do + define_model "ModelWithAfterCommitCallbacks" do + class_attribute :after_commit_callbacks_received + + after_commit do + self.class.after_commit_callbacks_received = true + end + end + + FactoryBot.define do + factory :model_with_after_commit_callbacks + end + + FactoryBot.lint + expect(ModelWithAfterCommitCallbacks.after_commit_callbacks_received).to be true + end + it "does not raise when all factories are valid" do define_model "User", name: :string do validates :name, presence: true