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.

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Recorder

Returns a new instance of Recorder.



16
17
18
19
20
21
22
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 16

def initialize(klass)
  @message_chains = MessageChains.new
  @observed_methods = []
  @played_methods = {}
  @klass = klass
  @expectation_set = false
end

Instance Method Details

#should_not_receive(method_name, &block) ⇒ Object



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

def should_not_receive(method_name, &block)
  observe!(method_name)
  message_chains.add(method_name, NegativeExpectationChain.new(method_name, &block))
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.



56
57
58
59
60
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 56

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

#stub(method_name_or_method_map, &block) ⇒ Object

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

See Also:



28
29
30
31
32
33
34
35
36
37
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 28

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)
    message_chains.add(method_name_or_method_map, StubChain.new(method_name_or_method_map, &block))
  end
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:



44
45
46
47
48
49
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 44

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(*args, &block))
  end
end

#unstub(method_name) ⇒ Object

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

See Also:



71
72
73
74
75
76
77
# 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
  message_chains.remove_stub_chains_for!(method_name)
  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.



83
84
85
86
87
# File 'lib/rspec/mocks/any_instance/recorder.rb', line 83

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