Class: VerifiedDouble::RecordingDouble

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
RelaysToInternalDoubleReturningSelf
Defined in:
lib/verified_double/recording_double.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RelaysToInternalDoubleReturningSelf

relays_to_internal_double_returning_self

Constructor Details

#initialize(double, class_name, method_stubs = {}) ⇒ RecordingDouble

Returns a new instance of RecordingDouble.



13
14
15
16
17
18
19
20
21
# File 'lib/verified_double/recording_double.rb', line 13

def initialize(double, class_name, method_stubs={})
  @double = double
  @class_name = class_name

  super(@double)
  method_stubs.each do |method, output|
    self.stub(method).and_return(output)
  end
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



11
12
13
# File 'lib/verified_double/recording_double.rb', line 11

def class_name
  @class_name
end

Instance Method Details

#and_return(return_value) ⇒ Object



23
24
25
26
27
# File 'lib/verified_double/recording_double.rb', line 23

def and_return(return_value)
  self.method_signatures.last.return_values = [MethodSignatureValue.new(return_value)]
  @double_call.and_return(return_value)
  self
end

#class_double?Boolean

Returns:



29
30
31
# File 'lib/verified_double/recording_double.rb', line 29

def class_double?
  ! double.is_a?(RSpec::Mocks::Mock)
end

#doubleObject



33
34
35
# File 'lib/verified_double/recording_double.rb', line 33

def double
  __getobj__
end

#inspectObject



37
38
39
# File 'lib/verified_double/recording_double.rb', line 37

def inspect
  to_s
end

#method_operatorObject



41
42
43
# File 'lib/verified_double/recording_double.rb', line 41

def method_operator
  class_double? ? '.' : '#'
end

#method_signaturesObject



45
46
47
# File 'lib/verified_double/recording_double.rb', line 45

def method_signatures
  @method_signatures ||= []
end

#should_receive(method) ⇒ Object



49
50
51
52
53
# File 'lib/verified_double/recording_double.rb', line 49

def should_receive(method)
  add_method_signature method
  @double_call = double.should_receive(method)
  self
end

#stub(method) ⇒ Object



55
56
57
58
59
# File 'lib/verified_double/recording_double.rb', line 55

def stub(method)
  add_method_signature method
  @double_call = double.stub(method)
  self
end

#to_sObject



61
62
63
# File 'lib/verified_double/recording_double.rb', line 61

def to_s
  "#{VerifiedDouble}.of_#{class_double? ? 'class' : 'instance' }('#{class_name}')"
end

#with(*args) ⇒ Object



65
66
67
68
69
70
# File 'lib/verified_double/recording_double.rb', line 65

def with(*args)
  self.method_signatures.last.args =
    args.map{|arg| MethodSignatureValue.new(arg) }
  @double_call.with(*args)
  self
end