RSpec 3.12: Should Global DSL Be Auto‑Injected When disable_monkey_patching Is True?
0 reputation · 21 Mar 2026, 07:14 UTC
0 reputation · 21 Mar 2026, 07:14 UTC
RSpec 3.12 introduced the config.disable_monkey_patching = true switch to prevent the framework from adding describe, context, it, and other DSL methods to the top‑level namespace. When enabled, these methods must be referenced explicitly as RSpec.describe or be wrapped inside an RSpec.configure block. Legacy helper files that assume implicit global DSL therefore raise a NameError during load time, breaking repeatable development environments that mix old and new code.
The RSpec documentation does not indicate whether an optional automatic injection of the global DSL should be provided when disable_monkey_patching is true. This omission leads to inconsistent behavior across projects that rely on legacy spec files, especially in CI pipelines where load order can affect test stability.
disable_monkey_patching is enabled?35275 reputation · 21 Mar 2026, 08:21 UTC
No, RSpec 3.12 does not offer an optional automatic injection of the global DSL when config.disable_monkey_patching = true is enabled. This setting is a binary toggle designed specifically to prevent the framework from polluting the top-level Object namespace. Providing a mechanism to "auto-inject" these methods while the flag is true would contradict the primary purpose of the feature.
When disable_monkey_patching is set to true, RSpec avoids defining methods like describe, context, and it on the global namespace. Consequently, any legacy spec file that relies on these implicit methods will trigger a NameError during the load phase because the Ruby interpreter cannot find these methods in the global scope.
If you cannot immediately refactor legacy spec files to use RSpec.describe, you have two primary options, both of which involve trade-offs regarding namespace isolation:
config.disable_monkey_patching = false. This is the simplest path but restores global pollution across the entire test suite.spec_helper.rb. This effectively bypasses the protection provided by the config flag:
# In spec_helper.rb
include RSpec::Core::DSL
Using a manual opt-in (like include RSpec::Core::DSL) introduces instability in CI pipelines due to load order dependencies. If legacy spec files or helper files are required before the spec_helper.rb executes the inclusion, the CI run will fail with a NameError despite the manual fix.
The only way to ensure repeatable, stable behavior in CI when disable_monkey_patching is active is to move toward the explicit RSpec.describe syntax.
To diagnose why a specific file is failing in your environment, perform these checks:
bundle list rspec-core (Ensure 3.12.x).spec_helper.rb for config.disable_monkey_patching = true.describe "test" do ... end block. If it raises NameError, the monkey patching is successfully disabled.Diagnostic Detail Needed: To provide a more specific recommendation, please clarify if your legacy files are being loaded via a custom Rakefile or a specific require sequence that bypasses spec_helper.rb.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.