class RSpec::Mocks::NamedObjectReference

An implementation of rspec-mocks' reference interface. Used when a string is passed to {ExampleMethods#object_double}, and when a string, named class or named module is passed to {ExampleMethods#instance_double}, or {ExampleMethods#class_double}. Represents a reference to the object named (via a constant lookup) by the string. @see DirectObjectReference

Public Class Methods

new(const_name) click to toggle source

@param const_name [String] constant name

# File lib/rspec/mocks/object_reference.rb, line 111
def initialize(const_name)
  @const_name = const_name
end

Public Instance Methods

const_to_replace() click to toggle source

@return [String] the constant name to replace with a double.

# File lib/rspec/mocks/object_reference.rb, line 121
def const_to_replace
  @const_name
end
Also aliased as: description
defined?() click to toggle source

@return [Boolean] true if the named constant is defined, false otherwise.

# File lib/rspec/mocks/object_reference.rb, line 116
def defined?
  !!object
end
description()
Alias for: const_to_replace
target() click to toggle source

@return [Object, nil] the target of the verifying double (the named object), or

nil if it is not defined.
# File lib/rspec/mocks/object_reference.rb, line 128
def target
  object
end
when_loaded() { |object| ... } click to toggle source

Yields if the reference target is loaded, providing a generic mechanism to optionally run a bit of code only when a reference's target is loaded.

@yield [Object] the target object

# File lib/rspec/mocks/object_reference.rb, line 137
def when_loaded
  yield object if object
end

Private Instance Methods

object() click to toggle source
# File lib/rspec/mocks/object_reference.rb, line 143
def object
  return @object if defined?(@object)
  @object = Constant.original(@const_name).original_value
end