Class: RSpec::Mocks::MethodReference

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/mocks/method_reference.rb

Overview

Represents a method on an object that may or may not be defined. The method may be an instance method on a module or a method on any object.

Direct Known Subclasses

InstanceMethodReference, ObjectMethodReference

Instance Method Summary (collapse)

Constructor Details

- (MethodReference) initialize(object_reference, method_name)



9
10
11
12
# File 'lib/rspec/mocks/method_reference.rb', line 9

def initialize(object_reference, method_name)
  @object_reference = object_reference
  @method_name = method_name
end

Instance Method Details

- (Boolean) defined?

A method is defined if we are able to get a `Method` object for it. In that case, we can assert against metadata like the arity.



40
41
42
43
44
# File 'lib/rspec/mocks/method_reference.rb', line 40

def defined?
  @object_reference.when_loaded do |m|
    method_defined?(m)
  end
end

- (Boolean) implemented?

A method is implemented if sending the message does not result in a `NoMethodError`. It might be dynamically implemented by `method_missing`.



17
18
19
20
21
# File 'lib/rspec/mocks/method_reference.rb', line 17

def implemented?
  @object_reference.when_loaded do |m|
    method_implemented?(m)
  end
end

- (Boolean) unimplemented?

Returns true if we definitively know that sending the method will result in a `NoMethodError`.

This is not simply the inverse of `implemented?`: there are cases when we don't know if a method is implemented and both `implemented?` and `unimplemented?` will return false.



29
30
31
32
33
34
35
36
# File 'lib/rspec/mocks/method_reference.rb', line 29

def unimplemented?
  @object_reference.when_loaded do |m|
    return !implemented?
  end

  # If it's not loaded, then it may be implemented but we can't check.
  false
end

- (Object) visibility



52
53
54
55
56
57
58
59
60
# File 'lib/rspec/mocks/method_reference.rb', line 52

def visibility
  @object_reference.when_loaded do |m|
    return visibility_from(m)
  end

  # When it's not loaded, assume it's public. We don't want to
  # wrongly treat the method as private.
  :public
end

- (Object) with_signature



46
47
48
49
50
# File 'lib/rspec/mocks/method_reference.rb', line 46

def with_signature
  if original = original_method
    yield Support::MethodSignature.new(original)
  end
end