Skip to content

Fix build_stubbed when Rails is not loaded - #1864

Draft
neilvcarvalho wants to merge 1 commit into
mainfrom
nc/build-stubbed-without-rails
Draft

neilvcarvalho wants to merge 1 commit into
mainfrom
nc/build-stubbed-without-rails

Conversation

@neilvcarvalho

Copy link
Copy Markdown
Member

build_stubbed raises in a process that requires only factory_bot:

require "factory_bot"

class Plain
  attr_accessor :id, :created_at, :updated_at
end

FactoryBot.define { factory :plain }
FactoryBot.build_stubbed(:plain)
lib/factory_bot/strategy/stub.rb:105:in 'FactoryBot::Strategy::Stub#set_timestamps': undefined method 'current' for class Time (NoMethodError)

With Time.current available, the same script then fails one step later:

lib/factory_bot/strategy/stub.rb:119:in 'FactoryBot::Strategy::Stub#missing_created_at?': undefined method 'blank?' for nil (NoMethodError)

The stub strategy uses two Active Support core extensions that factory_bot never required. This adds active_support/core_ext/time/calculations and active_support/core_ext/object/blank beside the other Active Support requires in lib/factory_bot.rb.

Spec

The suite loads Active Record, so both extensions are always defined in its process. The new spec in spec/factory_bot/strategy/stub_without_rails_spec.rb runs the script above in a separate Ruby process and checks the id and timestamps. It fails when either require is removed.

Notes

  • Fold duplicate check into Registry #1862 also edits the top of NEWS.md, so the "Unreleased" heading added here may need a trivial rebase.
  • build, create, attributes_for and associations were checked by hand without Rails loaded and work on main. The other core extensions in use (symbolize_keys, extract_options!, the inflector) are loaded transitively by Active Support and are not changed here.

Verification

  • bundle exec rake all_specs (Ruby 4.0.10, default Gemfile): 313 unit and 457 acceptance examples, 4 scenarios, 0 failures
  • bundle exec standardrb: no offenses
  • BUNDLE_GEMFILE=gemfiles/7.0.gemfile bundle exec rake all_specs: 0 failures
  • BUNDLE_GEMFILE=gemfiles/main.gemfile bundle exec rake all_specs: 0 failures

Currently, build_stubbed raises NoMethodError in a process that
requires only factory_bot. The stub strategy calls Time.current and
blank?, which are Active Support core extensions that factory_bot never
required. They were only defined when the application had loaded
Rails or the extensions itself, which this suite always does through
Active Record.

This commit requires active_support/core_ext/time/calculations and
active_support/core_ext/object/blank next to the other Active Support
requires. The new spec runs build_stubbed in a separate Ruby process,
because the extensions are already loaded in the suite's own process.

There are no side effects for applications that load Rails, where both
files are already required.
@neilvcarvalho
neilvcarvalho marked this pull request as draft November 27, 2026 20:15

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant