Class: Mocktail::RecordsDemonstration

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mocktail/records_demonstration.rb,
lib/mocktail/sorbet/mocktail/records_demonstration.rb

Instance Method Summary collapse

Instance Method Details

#record(demonstration, demo_config) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mocktail/records_demonstration.rb', line 5

def record(demonstration, demo_config)
  cabinet = Mocktail.cabinet
  prior_call_count = Mocktail.cabinet.calls.dup.size

  begin
    cabinet.demonstration_in_progress = true
    ValidatesArguments.optional(demo_config.ignore_arity) do
      demonstration.call(Mocktail.matchers)
    end
  ensure
    cabinet.demonstration_in_progress = false
  end

  if prior_call_count + 1 == cabinet.calls.size
    cabinet.calls.pop
  elsif prior_call_count == cabinet.calls.size
    raise MissingDemonstrationError.new <<~MSG.tr("\n", " ")
      `stubs` & `verify` expect an invocation of a mocked method by a passed
      block, but no invocation occurred.
    MSG
  else
    raise AmbiguousDemonstrationError.new <<~MSG.tr("\n", " ")
      `stubs` & `verify` expect exactly one invocation of a mocked method,
      but #{cabinet.calls.size - prior_call_count} were detected. As a
      result, Mocktail doesn't know which invocation to stub or verify.
    MSG
  end
end