class RSpec::Mocks::ConstantMutator
Provides a means to stub constants.
Public Class Methods
Hides a constant.
@param (see RSpec::Mocks::ExampleMethods#hide_const)
@see RSpec::Mocks::ExampleMethods#hide_const @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).
# File lib/rspec/mocks/mutate_const.rb, line 127 def self.hide(constant_name) mutate(ConstantHider.new(constant_name, nil, {})) nil end
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.
@private
# File lib/rspec/mocks/mutate_const.rb, line 316 def self.mutate(mutator) ::RSpec::Mocks.space.register_constant_mutator(mutator) mutator.mutate end
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).
@api private
# File lib/rspec/mocks/mutate_const.rb, line 327 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
Stubs a constant.
@param (see RSpec::Mocks::ExampleMethods#stub_const) @option (see RSpec::Mocks::ExampleMethods#stub_const) @return (see RSpec::Mocks::ExampleMethods#stub_const)
@see RSpec::Mocks::ExampleMethods#stub_const @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).
# File lib/rspec/mocks/mutate_const.rb, line 107 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[:transfer_nested_constants])) value end