Repository navigation
Fix build_stubbed when Rails is not loaded - #1864
Draft
neilvcarvalho wants to merge 1 commit into
Draft
neilvcarvalho wants to merge 1 commit into
neilvcarvalho wants to merge 1 commit into
Conversation
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
marked this pull request as draft
November 27, 2026 20:15
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
build_stubbedraises in a process that requires onlyfactory_bot:With
Time.currentavailable, the same script then fails one step later:The stub strategy uses two Active Support core extensions that
factory_botnever required. This addsactive_support/core_ext/time/calculationsandactive_support/core_ext/object/blankbeside the other Active Support requires inlib/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.rbruns the script above in a separate Ruby process and checks the id and timestamps. It fails when either require is removed.Notes
NEWS.md, so the "Unreleased" heading added here may need a trivial rebase.build,create,attributes_forand 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 failuresbundle exec standardrb: no offensesBUNDLE_GEMFILE=gemfiles/7.0.gemfile bundle exec rake all_specs: 0 failuresBUNDLE_GEMFILE=gemfiles/main.gemfile bundle exec rake all_specs: 0 failures