Class: Facon::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-facon/proxy.rb

Overview

A proxy for mock objects. Stubs and expectations are set on this proxy.

Instance Method Summary collapse

Constructor Details

#initialize(target, name) ⇒ Proxy

Returns a new instance of Proxy.



12
13
14
15
16
17
18
19
# File 'lib/motion-facon/proxy.rb', line 12

def initialize(target, name)
  @target, @name = target, name
  @expectations = []
  @stubs = []
  @proxied_methods = []
  @error_generator = ErrorGenerator.new(target, name)
  @expectation_ordering = nil unless defined?(@expectation_ordering)
end

Instance Method Details

#add_expectation(expected_from, method, &block) ⇒ Object



29
30
31
32
33
34
# File 'lib/motion-facon/proxy.rb', line 29

def add_expectation(expected_from, method, &block)
  add_method(method)

  @expectations << Expectation.new(@error_generator, @expectation_ordering, expected_from, method, (block_given? ? block : nil), 1)
  @expectations.last
end

#add_negative_expectation(expected_from, method, &block) ⇒ Object



36
37
38
39
40
41
# File 'lib/motion-facon/proxy.rb', line 36

def add_negative_expectation(expected_from, method, &block)
  add_method(method)

  @expectations << NegativeExpectation.new(@error_generator, @expectation_ordering, expected_from, method, (block_given? ? block : nil))
  @expectations.last
end

#add_stub(expected_from, method) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/motion-facon/proxy.rb', line 21

def add_stub(expected_from, method)
  add_method(method)

  # A stub is really an expectation that can be called any number of times.
  @stubs.unshift(Expectation.new(@error_generator, @expectation_ordering, expected_from, method, nil, :any))
  @stubs.first
end

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



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/motion-facon/proxy.rb', line 43

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

#raise_unexpected_message_args_error(*args) ⇒ Object



8
9
10
# File 'lib/motion-facon/proxy.rb', line 8

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

#raise_unexpected_message_error(*args) ⇒ Object



4
5
6
# File 'lib/motion-facon/proxy.rb', line 4

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

#resetObject



61
62
63
64
65
66
# File 'lib/motion-facon/proxy.rb', line 61

def reset
  @expectations.clear
  @stubs.clear
  reset_proxied_methods
  @proxied_methods.clear
end

#verifyObject



55
56
57
58
59
# File 'lib/motion-facon/proxy.rb', line 55

def verify
  @expectations.each { |expectation| expectation.met? }
ensure
  reset
end