Class: RSpec::Mocks::ConstantMutator
- Inherits:
-
Object
- Object
- RSpec::Mocks::ConstantMutator
- 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)
- + (Object) find(name)
-
+ (Object) hide(constant_name)
Hides a constant.
-
+ (Object) mutate(mutator)
private
Uses the mutator to mutate (stub or hide) a constant.
-
+ (Object) mutators
private
The list of constant mutators that have been used for the current example.
-
+ (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) register_mutator(mutator) private
-
+ (Object) reset_all
private
Resets all stubbed constants.
-
+ (Object) stub(constant_name, value, options = {})
Stubs a constant.
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) find(name)
385 386 387 |
# File 'lib/rspec/mocks/mutate_const.rb', line 385 def self.find(name) mutators.find { |s| s.full_constant_name == name } end |
+ (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.
192 193 194 195 196 197 198 199 200 |
# File 'lib/rspec/mocks/mutate_const.rb', line 192 def self.hide(constant_name) space = RSpec::Mocks.space space.print_out_of_example_deprecation if space.outside_example return unless recursive_const_defined?(constant_name) mutate(ConstantHider.new(constant_name, nil, { })) nil end |
+ (Object) mutate(mutator)
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.
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.
354 355 356 357 |
# File 'lib/rspec/mocks/mutate_const.rb', line 354 def self.mutate(mutator) register_mutator(mutator) mutator.mutate end |
+ (Object) mutators
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.
The list of constant mutators that have been used for the current example.
376 377 378 |
# File 'lib/rspec/mocks/mutate_const.rb', line 376 def self.mutators @mutators ||= [] 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).
395 396 397 398 399 400 |
# File 'lib/rspec/mocks/mutate_const.rb', line 395 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) register_mutator(mutator)
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.
381 382 383 |
# File 'lib/rspec/mocks/mutate_const.rb', line 381 def self.register_mutator(mutator) mutators << mutator end |
+ (Object) reset_all
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.
Resets all stubbed constants. This is called automatically by rspec-mocks when an example finishes.
363 364 365 366 367 368 369 370 |
# File 'lib/rspec/mocks/mutate_const.rb', line 363 def self.reset_all # We use reverse order so that if the same constant # was stubbed multiple times, the original value gets # properly restored. mutators.reverse.each { |s| s.rspec_reset } mutators.clear 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.
169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/rspec/mocks/mutate_const.rb', line 169 def self.stub(constant_name, value, = {}) space = RSpec::Mocks.space space.print_out_of_example_deprecation if space.outside_example 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 |