Class: RSpec::Mocks::AnyInstance::Recorder
- Inherits:
-
Object
- Object
- RSpec::Mocks::AnyInstance::Recorder
- Defined in:
- 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.
Instance Method Summary (collapse)
- - (Boolean) already_observing?(method_name)
- - (Object) build_alias_method_name(method_name)
-
- (Recorder) initialize(klass)
constructor
A new instance of Recorder.
- - (Object) should_not_receive(method_name)
-
- (Object) should_receive(method_name)
Initializes the recording a message expectation to be played back against any instance of this object that invokes the submitted method.
-
- (Object) stub(method_name_or_method_map)
Initializes the recording a stub to be played back against any instance of this object that invokes the submitted method.
-
- (Object) stub_chain(*method_names_and_optional_return_values)
Initializes the recording a stub chain to be played back against any instance of this object that invokes the method matching the first argument.
-
- (Object) unstub(method_name)
Removes any previously recorded stubs, stub_chains or message expectations that use
method_name
. -
- (Object) verify
private
Used internally to verify that message expectations have been fulfilled.
Constructor Details
- (Recorder) initialize(klass)
A new instance of Recorder
16 17 18 19 20 21 22 23 |
# File '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 = {} @klass = klass @expectation_set = false end |
Instance Method Details
- (Boolean) already_observing?(method_name)
125 126 127 |
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 125 def already_observing?(method_name) @observed_methods.include?(method_name) end |
- (Object) build_alias_method_name(method_name)
121 122 123 |
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 121 def build_alias_method_name(method_name) "__#{method_name}_without_any_instance__" end |
- (Object) should_not_receive(method_name)
63 64 65 |
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 63 def should_not_receive(method_name, &block) should_receive(method_name, &block).never end |
- (Object) should_receive(method_name)
Initializes the recording a message expectation to be played back against any instance of this object that invokes the submitted method.
57 58 59 60 61 |
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 57 def should_receive(method_name, &block) @expectation_set = true observe!(method_name) .add(method_name, PositiveExpectationChain.new(self, method_name, &block)) end |
- (Object) stub(method_name_or_method_map)
Initializes the recording a stub to be played back against any instance of this object that invokes the submitted method.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 29 def stub(method_name_or_method_map, &block) if method_name_or_method_map.is_a?(Hash) method_name_or_method_map.each do |method_name, return_value| stub(method_name).and_return(return_value) end else observe!(method_name_or_method_map) .add(method_name_or_method_map, StubChain.new(self, method_name_or_method_map, &block)) end end |
- (Object) stub_chain(*method_names_and_optional_return_values)
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/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 |
- (Object) unstub(method_name)
Removes any previously recorded stubs, stub_chains or message
expectations that use method_name
.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 71 def unstub(method_name) unless @observed_methods.include?(method_name.to_sym) raise RSpec::Mocks::MockExpectationError, "The method `#{method_name}` was not stubbed or was already unstubbed" end .remove_stub_chains_for!(method_name) ::RSpec::Mocks.proxies_of(@klass).each do |proxy| stubs[method_name].each { |stub| proxy.remove_single_stub(method_name, stub) } end stubs[method_name].clear stop_observing!(method_name) unless .has_expectation?(method_name) end |
- (Object) verify
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.
87 88 89 90 91 |
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 87 def verify if @expectation_set && !.all_expectations_fulfilled? raise RSpec::Mocks::MockExpectationError, "Exactly one instance should have received the following message(s) but didn't: #{.unfulfilled_expectations.sort.join(', ')}" end end |