Class: RSpec::Mocks::AnyInstance::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb

Overview

Given a class ‘TheClass`, `TheClass.any_instance` returns a `Recorder`, which records stubs and message expectations for later playback on instances of `TheClass`.

Further constraints are stored in instances of [Chain](Chain).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Recorder

Returns a new instance of Recorder.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 16

def initialize(klass)
  @message_chains = MessageChains.new
  @stubs = Hash.new { |hash, key| hash[key] = [] }
  @observed_methods = []
  @played_methods = {}
  @backed_up_method_owner = {}
  @klass = klass
  @expectation_set = false

  return unless RSpec::Mocks.configuration.verify_partial_doubles?
  RSpec::Mocks.configuration.verifying_double_callbacks.each do |block|
    block.call(ObjectReference.for(klass))
  end
end

Instance Attribute Details

#klassObject (readonly)



14
15
16
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 14

def klass
  @klass
end

#message_chainsObject (readonly)



14
15
16
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 14

def message_chains
  @message_chains
end

#stubsObject (readonly)



14
15
16
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 14

def stubs
  @stubs
end

Instance Method Details

#already_observing?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 127

def already_observing?(method_name)
  @observed_methods.include?(method_name) || super_class_observing?(method_name)
end

#build_alias_method_name(method_name) ⇒ Object



122
123
124
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 122

def build_alias_method_name(method_name)
  "__#{method_name}_without_any_instance__"
end

#expect_chain(*method_names_and_optional_return_values, &block) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 53

def expect_chain(*method_names_and_optional_return_values, &block)
  @expectation_set = true
  normalize_chain(*method_names_and_optional_return_values) do |method_name, args|
    observe!(method_name)
    message_chains.add(method_name, ExpectChainChain.new(self, *args, &block))
  end
end

#instance_that_received(method_name) ⇒ Object



117
118
119
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 117

def instance_that_received(method_name)
  @played_methods[method_name]
end

#notify_received_message(_object, message, args, _blk) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 132

def notify_received_message(_object, message, args, _blk)
  has_expectation = false

  message_chains.each_unfulfilled_expectation_matching(message, *args) do |expectation|
    has_expectation = true
    expectation.expectation_fulfilled!
  end

  return unless has_expectation

  restore_method!(message)
  mark_invoked!(message)
end

#playback!(instance, method_name) ⇒ Object



109
110
111
112
113
114
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 109

def playback!(instance, method_name)
  RSpec::Mocks.space.ensure_registered(instance)
  message_chains.playback!(instance, method_name)
  @played_methods[method_name] = instance
  received_expected_message!(method_name) if message_chains.has_expectation?(method_name)
end

#should_not_receive(method_name, &block) ⇒ Object

The opposite of ‘should_receive`

See Also:

  • Methods#should_not_receive


75
76
77
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 75

def should_not_receive(method_name, &block)
  should_receive(method_name, &block).never
end

#should_receive(method_name, &block) ⇒ Object

Initializes the recording a message expectation to be played back against any instance of this object that invokes the submitted method.

See Also:

  • Methods#should_receive


66
67
68
69
70
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 66

def should_receive(method_name, &block)
  @expectation_set = true
  observe!(method_name)
  message_chains.add(method_name, PositiveExpectationChain.new(self, method_name, &block))
end

#stop_all_observation!Object



104
105
106
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 104

def stop_all_observation!
  @observed_methods.each { |method_name| restore_method!(method_name) }
end

#stub(method_name, &block) ⇒ Object

Initializes the recording a stub to be played back against any instance of this object that invokes the submitted method.

See Also:

  • Methods#stub


35
36
37
38
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 35

def stub(method_name, &block)
  observe!(method_name)
  message_chains.add(method_name, StubChain.new(self, method_name, &block))
end

#stub_chain(*method_names_and_optional_return_values, &block) ⇒ Object

Initializes the recording a stub chain to be played back against any instance of this object that invokes the method matching the first argument.

See Also:

  • Methods#stub_chain


45
46
47
48
49
50
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 45

def stub_chain(*method_names_and_optional_return_values, &block)
  normalize_chain(*method_names_and_optional_return_values) do |method_name, args|
    observe!(method_name)
    message_chains.add(method_name, StubChainChain.new(self, *args, &block))
  end
end

#unstub(method_name) ⇒ Object

Removes any previously recorded stubs, stub_chains or message expectations that use ‘method_name`.

See Also:

  • Methods#unstub


83
84
85
86
87
88
89
90
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 83

def unstub(method_name)
  unless @observed_methods.include?(method_name.to_sym)
    AnyInstance.error_generator.raise_method_not_stubbed_error(method_name)
  end
  message_chains.remove_stub_chains_for!(method_name)
  stubs[method_name].clear
  stop_observing!(method_name) unless message_chains.has_expectation?(method_name)
end

#verifyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Used internally to verify that message expectations have been fulfilled.



96
97
98
99
100
101
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb', line 96

def verify
  return unless @expectation_set
  return if message_chains.all_expectations_fulfilled?

  AnyInstance.error_generator.raise_second_instance_received_message_error(message_chains.unfulfilled_expectations)
end