Class: RSpec::Mocks::ConstantMutator

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

Overview

Provides a means to stub constants.

Class Method Summary collapse

Class Method Details

.hide(constant_name) ⇒ Object

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.

Parameters:

  • constant_name (String)

    The fully qualified name of the constant. The current constant scoping at the point of call is not considered.

See Also:



131
132
133
134
# File 'lib/rspec/mocks/mutate_const.rb', line 131

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

.raise_on_invalid_constObject

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).



331
332
333
334
335
336
# File 'lib/rspec/mocks/mutate_const.rb', line 331

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

.stub(constant_name, value, options = {}) ⇒ Object

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.

Parameters:

  • constant_name (String)

    The fully qualified name of the constant. The current constant scoping at the point of call is not considered.

  • value (Object)

    The value to make the constant refer to. When the example completes, the constant will be restored to its prior state.

  • options (Hash) (defaults to: {})

    Stubbing options.

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).

Returns:

  • (Object)

    the stubbed value of the constant

See Also:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rspec/mocks/mutate_const.rb', line 107

def self.stub(constant_name, value, options={})
  unless String === constant_name
    raise ArgumentError, "`stub_const` requires a String, but you provided a #{constant_name.class.name}"
  end

  mutator = if recursive_const_defined?(constant_name, &raise_on_invalid_const)
              DefinedConstantReplacer
            else
              UndefinedConstantSetter
            end

  mutate(mutator.new(constant_name, value, options[:transfer_nested_constants]))
  value
end