Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FactoryBot introduces ActiveSupport monkey patching to Hashes #1661

Open
jedschneider opened this issue Jun 3, 2024 · 1 comment
Open

FactoryBot introduces ActiveSupport monkey patching to Hashes #1661

jedschneider opened this issue Jun 3, 2024 · 1 comment
Labels

Comments

@jedschneider
Copy link

Description

We discovered that our runtime code in production had an expectation to be able to call symbolize_keys on a Ruby hash even though we had no dependency on activesupport. The tests flexed this code and passed. I was able to produce an example script that illustrates that factory_bot is injecting code into base classes which can mask production issues during test.

Reproduction Steps

begin
  {}.symbolize_keys
rescue NoMethodError => e
  puts "\e[31m------ WARNING ----- Hashes do not respond to symbolize_keys ---------\e[0m"
end

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'
  group :development, :test do
    gem 'factory_bot'
  end
end

require 'factory_bot'

puts "FactoryBot Version: #{FactoryBot::VERSION}"

begin
  {}.symbolize_keys
  puts "\e[31m------ WARNING ----- Hashes now respond to symbolize_keys ---------"
  puts "This behavior is introduced by ActiveSupport\e[0m"
rescue NoMethodError => e
  puts " ------------ EXPECTED ---- Hashes still do not respond to symbolize_keys"
end

Expected behavior

FactoryBot should not change behavior of classes it does not own.

Actual behavior

the EXPECTED line prints in the above script.

System configuration

factory_bot version: 6.4.6
rails version: NA
ruby version: ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin23]

@colinross
Copy link
Contributor

FactoryBot depends on ActiveSupport, and specifically calls in parts of it's core_ext for Hash objects.
This isn't a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants