Add config option to disable all monkey patching
Created by: myronmarston
This is the last thing mentioned in my the plan for rspec 3 blog post that we haven't done. Currently disabling monkey patching involves setting all of this config:
RSpec.configure do |rspec|
rspec.expose_dsl_globally = false
rspec.mock_with :rspec do |mocks|
mocks.syntax = :expect
# not strictly necessary but listing it for completeness
mocks.patch_marshal_to_support_partial_doubles = true
end
rspec.mock_with :rspec do |expectations|
expectations.syntax = :expect
end
end
That's a lot to type. We should expose a single config option that will take care of all of it:
RSpec.configure do |rspec|
rspec.disable_monkey_patching = true
# or, if we're not going to support changing it to false:
rspec.disable_monkey_patching!
end
One gotcha with this config option is that disable_monkey_patching
as a method name does not suggest that it will configure rspec to use rspec-mocks and rspec-expectations. I think that it should only configure rspec-mocks and rspec-expectations if the user is using those (either explicitly or implicitly by not setting mock_with
or expect_with
to anything else).
When the first example group is defined, we default mock_with
and expect_with
to the rspec defaults if the user hasn't set it to something else:
...so I think that this new config options should behave as follows:
- Set
expose_dsl_globally = false
. - If rspec-mocks and/or rspec-expectations have already been loaded, configure them as well at that time.
- Otherwise, store a bit of state that
RSpec.configuration.configure_mock_framework
andRSpec.configuration.configure_expectation_framework
will use to configure rspec-mocks and rspec-expectations to do no monkey patching.
It should be noted that if the user uses this option and mock_with :mocha
(or similar) that they will still have monkey patching active in their test environment from mocha, but I believe that's the correct behavior. It simply disables monkey patching on whatever pieces of rspec the user is using.