Class: RSpec::Mocks::ConstantMutator

Inherits:
Object
  • Object
show all
Extended by:
RecursiveConstMethods
Defined in:
lib/rspec/mocks/mutate_const.rb

Overview

Provides a means to stub constants.

Defined Under Namespace

Classes: BaseMutator, ConstantHider, DefinedConstantReplacer, UndefinedConstantSetter

Class Method Summary (collapse)

Methods included from RecursiveConstMethods

const_defined_on?, constants_defined_on, get_const_defined_on, normalize_const_name, recursive_const_defined?, recursive_const_get

Class Method Details

+ (Object) hide(constant_name)

Note:

It's recommended that you use `hide_const` in your examples. This is an alternate public API that is provided so you can hide constants in other contexts (e.g. helper classes).

Hides a constant.



193
194
195
196
# File 'lib/rspec/mocks/mutate_const.rb', line 193

def self.hide(constant_name)
  mutate(ConstantHider.new(constant_name, nil, { }))
  nil
end

+ (Object) mutate(mutator)

Uses the mutator to mutate (stub or hide) a constant. Ensures that the mutator is correctly registered so it can be backed out at the end of the test.



367
368
369
370
# File 'lib/rspec/mocks/mutate_const.rb', line 367

def self.mutate(mutator)
  ::RSpec::Mocks.space.register_constant_mutator(mutator)
  mutator.mutate
end

+ (Object) raise_on_invalid_const

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Used internally by the constant stubbing to raise a helpful error when a constant like “A::B::C” is stubbed and A::B is not a module (and thus, it's impossible to define “A::B::C” since only modules can have nested constants).



378
379
380
381
382
383
# File 'lib/rspec/mocks/mutate_const.rb', line 378

def self.raise_on_invalid_const
  lambda do |const_name, failed_name|
    raise "Cannot stub constant #{failed_name} on #{const_name} " +
          "since #{const_name} is not a module."
  end
end

+ (Object) stub(constant_name, value, options = {})

Note:

It's recommended that you use `stub_const` in your examples. This is an alternate public API that is provided so you can stub constants in other contexts (e.g. helper classes).

Stubs a constant.

Options Hash (options):

  • :transfer_nested_constants (Boolean, Array<Symbol>)

    Determines what nested constants, if any, will be transferred from the original value of the constant to the new value of the constant. This only works if both the original and new values are modules (or classes).

See Also:



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rspec/mocks/mutate_const.rb', line 170

def self.stub(constant_name, value, options = {})
  mutator = if recursive_const_defined?(constant_name, &raise_on_invalid_const)
    DefinedConstantReplacer
  else
    UndefinedConstantSetter
  end

  mutate(mutator.new(constant_name, value, options.fetch(
    :transfer_nested_constants,
    RSpec::Mocks.configuration.transfer_nested_constants?
  )))
  value
end