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

Inherits:
Object
  • Object
show all
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](Chain).

See Also:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Recorder) initialize(klass)

Returns 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 Attribute Details

- (Object) klass (readonly)



14
15
16
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 14

def klass
  @klass
end

- (Object) message_chains (readonly)



14
15
16
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 14

def message_chains
  @message_chains
end

- (Object) stubs (readonly)



14
15
16
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 14

def stubs
  @stubs
end

Instance Method Details

- (Boolean) already_observing?(method_name)

Returns:

  • (Boolean)


120
121
122
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 120

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

- (Object) build_alias_method_name(method_name)



115
116
117
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 115

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

- (Object) expect_chain(*method_names_and_optional_return_values, &block)



47
48
49
50
51
52
53
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 47

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

- (Object) instance_that_received(method_name)



110
111
112
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 110

def instance_that_received(method_name)
  @played_methods[method_name]
end

- (Object) playback!(instance, method_name)



102
103
104
105
106
107
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 102

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

- (Object) should_not_receive(method_name, &block)

The opposite of `should_receive`

See Also:

  • Methods#should_not_receive


69
70
71
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 69

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

- (Object) should_receive(method_name, &block)

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


60
61
62
63
64
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 60

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

- (Object) stop_all_observation!



97
98
99
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 97

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

- (Object) stub(method_name, &block)

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

See Also:

  • Methods#stub


29
30
31
32
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 29

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

- (Object) stub_chain(*method_names_and_optional_return_values, &block)

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


39
40
41
42
43
44
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 39

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

- (Object) unstub(method_name)

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

See Also:

  • Methods#unstub


77
78
79
80
81
82
83
84
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 77

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
  message_chains.remove_stub_chains_for!(method_name)
  stubs[method_name].clear
  stop_observing!(method_name) unless message_chains.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.



90
91
92
93
94
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 90

def verify
  if @expectation_set && !message_chains.all_expectations_fulfilled?
    raise RSpec::Mocks::MockExpectationError, "Exactly one instance should have received the following message(s) but didn't: #{message_chains.unfulfilled_expectations.sort.join(', ')}"
  end
end