Skip to content

RSpec zero monkey patching mode

Description

The plan for RSpec 4.0 is to disable monkey patching, references:

We should stop using monkey patching and enforce zero monkey patching mode in Rspec.

First, we need to change the source code:

# bad
require 'spec_helper'

describe 'specs here' do
  it 'passes' do
  end
end
# good
require 'spec_helper'

RSpec.describe 'specs here' do
  it 'passes' do
  end
end

Then we have to enable disable_monkey_patching to stop monkey patching RSpec. This configuration will trigger a method missing error to prevent us to keep using the monkey patching methods.

RSpec.configure do |config|
  config.disable_monkey_patching!
end

Having that configuration, it will raise a method missing error because RSpec is not adding methods to the Object class.

NoMethodError:
  undefined method `describe' for main:Object

A possible tool to help with this work could be Transpec, see https://github.com/yujinakayama/transpec#monkey-patched-example-groups

$ transpec --convert example_group --skip-dynamic-analysis
Edited by Arturo Herrero