Class: FlexMock::ExpectationDirector

Inherits:
Object
  • Object
show all
Defined in:
lib/flexmock/expectation_director.rb

Overview

The expectation director is responsible for routing calls to the correct expectations for a given argument list.

Instance Method Summary collapse

Constructor Details

#initialize(sym) ⇒ ExpectationDirector

Create an ExpectationDirector for a mock object.



23
24
25
26
27
28
# File 'lib/flexmock/expectation_director.rb', line 23

def initialize(sym)
  @sym = sym
  @expectations = []
  @defaults = []
  @expected_order = nil
end

Instance Method Details

#<<(expectation) ⇒ Object

Append an expectation to this director.



49
50
51
# File 'lib/flexmock/expectation_director.rb', line 49

def <<(expectation)
  @expectations << expectation
end

#call(args, call_record = nil) ⇒ Object

Invoke the expectations for a given set of arguments.

First, look for an expectation that matches the arguments and is eligible to be called. Failing that, look for a expectation that matches the arguments (at this point it will be ineligible, but at least we will get a good failure message). Finally, check for expectations that don’t have any argument matching criteria.



38
39
40
41
42
43
44
45
46
# File 'lib/flexmock/expectation_director.rb', line 38

def call(args, call_record=nil)
  exp = find_expectation(*args)
  call_record.expectation = exp if call_record
  FlexMock.check(
    "no matching handler found for " +
    FlexMock.format_call(@sym, args)) { ! exp.nil? }
  returned_value = exp.verify_call(*args)
  returned_value
end

#defaultify_expectation(exp) ⇒ Object

Move the last defined expectation a default.



72
73
74
75
76
77
78
79
80
# File 'lib/flexmock/expectation_director.rb', line 72

def defaultify_expectation(exp) # :nodoc:
  last_exp = @expectations.last
  if last_exp != exp
    fail UsageError,
      "Cannot make a previously defined expection into a default"
  end
  @expectations.pop
  @defaults << exp
end

#find_expectation(*args) ⇒ Object

Find an expectation matching the given arguments.



54
55
56
57
58
59
60
# File 'lib/flexmock/expectation_director.rb', line 54

def find_expectation(*args) # :nodoc:
  if @expectations.empty?
    find_expectation_in(@defaults, *args)
  else
    find_expectation_in(@expectations, *args)
  end
end

#flexmock_verifyObject

Do the post test verification for this director. Check all the expectations. Only check the default expecatations if there are no non-default expectations.



65
66
67
68
69
# File 'lib/flexmock/expectation_director.rb', line 65

def flexmock_verify         # :nodoc:
  (@expectations.empty? ? @defaults : @expectations).each do |exp|
    exp.flexmock_verify
  end
end