Class: RSpec::Mocks::ConstantMutator
- Inherits:
-
Object
- Object
- RSpec::Mocks::ConstantMutator
- Extended by:
- Support::RecursiveConstMethods
- Defined in:
- lib/rspec/mocks/mutate_const.rb
Overview
Provides a means to stub constants.
Class Method Summary (collapse)
-
+ (Object) hide(constant_name)
Hides a constant.
-
+ (Object) raise_on_invalid_const
private
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).
-
+ (Object) stub(constant_name, value, options = {})
Stubs a constant.
Class Method Details
+ (Object) 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.
116 117 118 119 |
# File 'lib/rspec/mocks/mutate_const.rb', line 116 def self.hide(constant_name) mutate(ConstantHider.new(constant_name, nil, {})) nil 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).
316 317 318 319 320 321 |
# File 'lib/rspec/mocks/mutate_const.rb', line 316 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 = {})
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.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rspec/mocks/mutate_const.rb', line 96 def self.stub(constant_name, value, ={}) 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 |