Class: RSpec::Mocks::AnyInstance::Recorder
- 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
- #klass ⇒ Object readonly
- #message_chains ⇒ Object readonly
- #stubs ⇒ Object readonly
Instance Method Summary collapse
- #already_observing?(method_name) ⇒ Boolean
- #build_alias_method_name(method_name) ⇒ Object
- #expect_chain(*method_names_and_optional_return_values, &block) ⇒ Object
-
#initialize(klass) ⇒ Recorder
constructor
A new instance of Recorder.
- #instance_that_received(method_name) ⇒ Object
- #notify_received_message(_object, message, args, _blk) ⇒ Object
- #playback!(instance, method_name) ⇒ Object
-
#should_not_receive(method_name, &block) ⇒ Object
The opposite of ‘should_receive`.
-
#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.
- #stop_all_observation! ⇒ Object
-
#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.
-
#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.
-
#unstub(method_name) ⇒ Object
Removes any previously recorded stubs, stub_chains or message expectations that use ‘method_name`.
-
#verify ⇒ Object
private
Used internally to verify that message expectations have been fulfilled.
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..each do |block| block.call(ObjectReference.for(klass)) end end |
Instance Attribute Details
#klass ⇒ Object (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_chains ⇒ Object (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 end |
#stubs ⇒ Object (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
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) .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 (_object, , args, _blk) has_expectation = false .each_unfulfilled_expectation_matching(, *args) do |expectation| has_expectation = true expectation.expectation_fulfilled! end return unless has_expectation restore_method!() mark_invoked!() 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) .playback!(instance, method_name) @played_methods[method_name] = instance (method_name) if .has_expectation?(method_name) end |
#should_not_receive(method_name, &block) ⇒ Object
The opposite of ‘should_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.
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) .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.
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) .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.
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) .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`.
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 .remove_stub_chains_for!(method_name) stubs[method_name].clear stop_observing!(method_name) unless .has_expectation?(method_name) end |
#verify ⇒ Object
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 .all_expectations_fulfilled? AnyInstance.error_generator.(.unfulfilled_expectations) end |