Class: RSpec::Crispy::CrispyFeatures::CrispyHaveReceived

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/crispy/crispy_features.rb

Direct Known Subclasses

NTimes

Defined Under Namespace

Classes: NTimes

Instance Method Summary collapse

Constructor Details

#initialize(method_name, *arguments) ⇒ CrispyHaveReceived

Returns a new instance of CrispyHaveReceived.



35
36
37
38
# File 'lib/rspec/crispy/crispy_features.rb', line 35

def initialize method_name, *arguments
  @method_name = method_name
  @arguments = arguments
end

Instance Method Details

#actually_received_messages_for_failure_messageObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rspec/crispy/crispy_features.rb', line 79

def actually_received_messages_for_failure_message
  if @spy_of_subject.received_messages.empty?
    "Actually, it has received no messages.\n".freeze
  else
    result = "Actually, it has received these messages:\n"
    @spy_of_subject.received_messages.each do|received_message|
      arguments_for_message = received_message.arguments.map(&:inspect).join(', '.freeze)
      # TODO: which instance actually received the message for ClassSpy
      result << "  it.#{received_message.method_name}(#{arguments_for_message})\n"
    end
    result
  end
end

#failure_messageObject



62
63
64
65
66
67
68
69
# File 'lib/rspec/crispy/crispy_features.rb', line 62

def failure_message
  @spy_of_subject.stop
  result = "Expected #{@subject.inspect} to have received :#@method_name method"
  result << " with #@arguments" unless @arguments.empty?
  result << ".\n"
  result << actually_received_messages_for_failure_message
  result
end

#failure_message_when_negatedObject



71
72
73
74
75
76
77
# File 'lib/rspec/crispy/crispy_features.rb', line 71

def failure_message_when_negated
  @spy_of_subject.stop
  result = "Expected #{@subject.inspect} NOT to have received :#@method_name method"
  result << " with #@arguments" unless @arguments.empty?
  result << ". But actually received.\n".freeze
  result
end

#matched_spy?(spy) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/rspec/crispy/crispy_features.rb', line 50

def matched_spy? spy
  spy.received? @method_name, *@arguments
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/rspec/crispy/crispy_features.rb', line 44

def matches?(subject)
  @subject = subject
  @spy_of_subject = spy_of_subject subject
  matched_spy?(@spy_of_subject)
end

#nameObject



40
41
42
# File 'lib/rspec/crispy/crispy_features.rb', line 40

def name
  'have_received'.freeze
end

#onceObject



54
55
56
# File 'lib/rspec/crispy/crispy_features.rb', line 54

def once
  times 1
end

#spy_of_subject(subject) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/rspec/crispy/crispy_features.rb', line 93

def spy_of_subject subject
  # Don't use instance_of? method. it records received_messages.
  if CrispyExpectAnyInstanceOf === subject
    subject.get_spy_of_instances
  else
    ::Crispy.spy(subject)
  end
end

#times(n) ⇒ Object



58
59
60
# File 'lib/rspec/crispy/crispy_features.rb', line 58

def times n
  NTimes.new n, @method_name, *@arguments
end