Logo: Relish

  1. Sign up
  2. Sign in

Project: RSpec Mocks 3.1

Old syntax

Historically, rspec-mocks has used a monkey-patched syntax to allow you to mock or stub any object:


Unfortunately, this is prone to weird, confusing failures when applied to delegate/proxy objects. For a method like stub to work properly, it must be defined on every object in the
system, but RSpec does not own every object in the system and cannot ensure that it always
works consistently.

For this reason, in RSpec 2.14, we introduced a new syntax that avoids monkey patching
altogether. It's the syntax shown in all examples of this documentation outside of this
directory. As of RSpec 3, we consider this to be the main, recommended syntax of rspec-
mocks. The old monkey-patched syntax continues to work, but you will get a deprecation
warning if you use it without explicitly opting-in to it:

# If you're using rspec-core:
RSpec.configure do |config|
  config.mock_with :rspec do |mocks|
    mocks.syntax = :should

# Or, if you're using rspec-mocks in another context:
RSpec::Mocks.configuration.syntax = :should

We have no plans to ever kill the old syntax, but we may extract it into an external gem in
RSpec 4.

If you have an old project that uses the old syntax and you want to update it to the current
syntax, checkout transpec.


  1. `stub`
  2. `should_receive`
  3. `any_instance`
  4. `stub_chain`
  5. `unstub`

Last published about 2 years ago by myronmarston.