123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Mocks::NamedObjectReference

Relationships & Source Files
Inherits: Object
Defined in: rspec-mocks/lib/rspec/mocks/object_reference.rb

Overview

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.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(const_name) ⇒ NamedObjectReference

Parameters:

  • const_name (String)

    constant name

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/object_reference.rb', line 111

def initialize(const_name)
  @const_name = const_name
end

Instance Attribute Details

#defined?Boolean (readonly)

Returns:

  • (Boolean)

    true if the named constant is defined, false otherwise.

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/object_reference.rb', line 116

def defined?
  !!object
end

Instance Method Details

#const_to_replaceString Also known as: #description

Returns:

  • (String)

    the constant name to replace with a double.

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/object_reference.rb', line 121

def const_to_replace
  @const_name
end

#description

Alias for #const_to_replace.

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/object_reference.rb', line 124

alias description const_to_replace

#object (private)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/object_reference.rb', line 143

def object
  return @object if defined?(@object)
  @object = Constant.original(@const_name).original_value
end

#targetObject?

Returns:

  • (Object, nil)

    the target of the verifying double (the named object), or nil if it is not defined.

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/object_reference.rb', line 128

def target
  object
end

#when_loaded {|Object| ... }

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.

Yields:

  • (Object)

    the target object

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/object_reference.rb', line 137

def when_loaded
  yield object if object
end