Class: Spec::Mocks::MockHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/mocks/mock_handler.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :null_object => false,
  :auto_verify => true
}

Instance Method Summary collapse

Constructor Details

#initialize(target, name, options = {}) ⇒ MockHandler

Returns a new instance of MockHandler.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/spec/mocks/mock_handler.rb', line 9

def initialize(target, name, options={})
  @target = target
  @name = name
  @error_generator = ErrorGenerator.new target, name
  @expectation_ordering = OrderGroup.new @error_generator
  @expectations = []
  @messages_received = []
  @stubs = []
  @proxied_methods = []
  @options = options ? DEFAULT_OPTIONS.dup.merge(options) : DEFAULT_OPTIONS
end

Instance Method Details

#add_message_expectation(expected_from, sym, opts = {}, &block) ⇒ Object



25
26
27
28
29
# File 'lib/spec/mocks/mock_handler.rb', line 25

def add_message_expectation(expected_from, sym, opts={}, &block)
  __add expected_from, sym, block
  @expectations << MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil, 1, opts)
  @expectations.last
end

#add_negative_message_expectation(expected_from, sym, &block) ⇒ Object



31
32
33
34
35
# File 'lib/spec/mocks/mock_handler.rb', line 31

def add_negative_message_expectation(expected_from, sym, &block)
  __add expected_from, sym, block
  @expectations << NegativeMessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil)
  @expectations.last
end

#add_stub(expected_from, sym) ⇒ Object



37
38
39
40
41
# File 'lib/spec/mocks/mock_handler.rb', line 37

def add_stub(expected_from, sym)
  __add expected_from, sym, nil
  @stubs.unshift MethodStub.new(@error_generator, @expectation_ordering, expected_from, sym, nil)
  @stubs.first
end

#has_negative_expectation?(sym) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/spec/mocks/mock_handler.rb', line 63

def has_negative_expectation?(sym)
  @expectations.detect {|expectation| expectation.negative_expectation_for?(sym)}
end

#message_received(sym, *args, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/spec/mocks/mock_handler.rb', line 67

def message_received(sym, *args, &block)
  if expectation = find_matching_expectation(sym, *args)
    expectation.invoke(args, block)
  elsif stub = find_matching_method_stub(sym)
    stub.invoke([], block)
  elsif expectation = find_almost_matching_expectation(sym, *args)
    raise_unexpected_message_args_error(expectation, *args) unless has_negative_expectation?(sym) unless null_object?
  else
    @target.send :method_missing, sym, *args, &block
  end
end

#null_object?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/spec/mocks/mock_handler.rb', line 21

def null_object?
  @options[:null_object]
end

#raise_unexpected_message_args_error(expectation, *args) ⇒ Object



79
80
81
# File 'lib/spec/mocks/mock_handler.rb', line 79

def raise_unexpected_message_args_error(expectation, *args)
  @error_generator.raise_unexpected_message_args_error expectation, *args
end

#raise_unexpected_message_error(sym, *args) ⇒ Object



83
84
85
# File 'lib/spec/mocks/mock_handler.rb', line 83

def raise_unexpected_message_error(sym, *args)
  @error_generator.raise_unexpected_message_error sym, *args
end

#received_message?(sym, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/spec/mocks/mock_handler.rb', line 58

def received_message?(sym, *args, &block)
  return true if @messages_received.find {|array| array == [sym, args, block]}
  return false
end

#resetObject



51
52
53
54
55
56
# File 'lib/spec/mocks/mock_handler.rb', line 51

def reset
  clear_expectations
  clear_stubs
  reset_proxied_methods
  clear_proxied_methods
end

#verifyObject

:nodoc:



43
44
45
46
47
48
49
# File 'lib/spec/mocks/mock_handler.rb', line 43

def verify #:nodoc:
  begin
    verify_expectations
  ensure
    reset
  end
end