Class: RR::Double

Inherits:
Object
  • Object
show all
Includes:
Space::Reader
Defined in:
lib/rr/double.rb

Overview

RR::Double is the use case for a method call. It has the ArgumentEqualityExpectation, TimesCalledExpectation, and the implementation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Space::Reader

#space

Constructor Details

#initialize(double_injection, definition) ⇒ Double

Returns a new instance of Double.



22
23
24
25
26
27
28
29
30
# File 'lib/rr/double.rb', line 22

def initialize(double_injection, definition)
  @double_injection = double_injection
  @definition = definition
  @times_called = 0
  @times_called_expectation = Expectations::TimesCalledExpectation.new(self)
  definition.double = self
  verify_method_signature if definition.verify_method_signature?
  double_injection.register_double self
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



19
20
21
# File 'lib/rr/double.rb', line 19

def definition
  @definition
end

#double_injectionObject (readonly)

Returns the value of attribute double_injection.



19
20
21
# File 'lib/rr/double.rb', line 19

def double_injection
  @double_injection
end

#times_calledObject (readonly)

Returns the value of attribute times_called.



19
20
21
# File 'lib/rr/double.rb', line 19

def times_called
  @times_called
end

#times_called_expectationObject (readonly)

Returns the value of attribute times_called_expectation.



19
20
21
# File 'lib/rr/double.rb', line 19

def times_called_expectation
  @times_called_expectation
end

Class Method Details

.formatted_name(method_name, args) ⇒ Object



7
8
9
10
# File 'lib/rr/double.rb', line 7

def formatted_name(method_name, args)
  formatted_errors = args.collect {|arg| arg.inspect}.join(', ')
  "#{method_name}(#{formatted_errors})"
end

.list_message_part(doubles) ⇒ Object



12
13
14
15
16
# File 'lib/rr/double.rb', line 12

def list_message_part(doubles)
  doubles.collect do |double|
    "- #{formatted_name(double.method_name, double.expected_arguments)}"
  end.join("\n")
end

Instance Method Details

#attempt?Boolean

Double#attempt? returns true when the TimesCalledExpectation is satisfied.

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/rr/double.rb', line 62

def attempt?
  verify_times_matcher_is_set
  times_called_expectation.attempt?
end

#call(double_injection, *args, &block) ⇒ Object

Double#call calls the Double’s implementation. The return value of the implementation is returned.

A TimesCalledError is raised when the times called exceeds the expected TimesCalledExpectation.



37
38
39
40
41
42
43
44
45
46
# File 'lib/rr/double.rb', line 37

def call(double_injection, *args, &block)
  if verbose?
    puts Double.formatted_name(double_injection.method_name, args)
  end
  times_called_expectation.attempt if definition.times_matcher
  space.verify_ordered_double(self) if ordered?
  yields!(block)
  return_value = call_implementation(double_injection, *args, &block)
  definition.after_call_proc ? extract_subject_from_return_value(definition.after_call_proc.call(return_value)) : return_value
end

#exact_match?(*arguments) ⇒ Boolean

Double#exact_match? returns true when the passed in arguments exactly match the ArgumentEqualityExpectation arguments.

Returns:

  • (Boolean)


50
51
52
# File 'lib/rr/double.rb', line 50

def exact_match?(*arguments)
  definition.exact_match?(*arguments)
end

#expected_argumentsObject

The Arguments that this Double expects



87
88
89
90
# File 'lib/rr/double.rb', line 87

def expected_arguments
  verify_argument_expectation_is_set
  argument_expectation.expected_arguments
end

#formatted_nameObject



97
98
99
# File 'lib/rr/double.rb', line 97

def formatted_name
  self.class.formatted_name(method_name, expected_arguments)
end

#method_nameObject

The method name that this Double is attatched to



82
83
84
# File 'lib/rr/double.rb', line 82

def method_name
  double_injection.method_name
end

#terminal?Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/rr/double.rb', line 76

def terminal?
  verify_times_matcher_is_set
  times_called_expectation.terminal?
end

#times_matcherObject

The TimesCalledMatcher for the TimesCalledExpectation



93
94
95
# File 'lib/rr/double.rb', line 93

def times_matcher
  definition.times_matcher
end

#verifyObject

Double#verify verifies the the TimesCalledExpectation is satisfied for this double. A TimesCalledError is raised if the TimesCalledExpectation is not met.



70
71
72
73
74
# File 'lib/rr/double.rb', line 70

def verify
  verify_times_matcher_is_set
  times_called_expectation.verify!
  true
end

#wildcard_match?(*arguments) ⇒ Boolean

Double#wildcard_match? returns true when the passed in arguments wildcard match the ArgumentEqualityExpectation arguments.

Returns:

  • (Boolean)


56
57
58
# File 'lib/rr/double.rb', line 56

def wildcard_match?(*arguments)
  definition.wildcard_match?(*arguments)
end