Class: RSpec::Mocks::ConstantMutator
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Inherits: | Object |
Defined in: | rspec-mocks/lib/rspec/mocks/mutate_const.rb |
Overview
Provides a means to stub constants.
Class Method Summary
-
.hide(constant_name)
Hides a constant.
-
.mutate(mutator)
Internal use only
Internal use only
Uses the mutator to mutate (stub or hide) a constant.
-
.raise_on_invalid_const
Internal use only
Internal use only
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).
-
.stub(constant_name, value, options = {}) ⇒ Object
Stubs a constant.
::RSpec::Support::RecursiveConstMethods
- Extended
const_defined_on? | See additional method definition at line 31. |
constants_defined_on | See additional method definition at line 41. |
get_const_defined_on | See additional method definition at line 35. |
normalize_const_name, recursive_const_defined?, recursive_const_get |
Class Method Details
.hide(constant_name)
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.
# File 'rspec-mocks/lib/rspec/mocks/mutate_const.rb', line 131
def self.hide(constant_name) mutate(ConstantHider.new(constant_name, nil, {})) nil end
.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.
.raise_on_invalid_const
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).
# File 'rspec-mocks/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
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.
# File 'rspec-mocks/lib/rspec/mocks/mutate_const.rb', line 107
def self.stub(constant_name, value, ={}) 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, [:transfer_nested_constants])) value end