Class: FlexMock::ExpectationDirector

Inherits:
Object
  • Object
show all
Defined in:
lib/gems/flexmock-0.8.3/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/gems/flexmock-0.8.3/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.



46
47
48
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/expectation_director.rb', line 46

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

#call(*args) ⇒ Object

Invoke the expectations for a given set of arguments.

First, look for an expectation that matches the arguements 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
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/expectation_director.rb', line 38

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

#defaultify_expectation(exp) ⇒ Object

Move the last defined expectation a default.



69
70
71
72
73
74
75
76
77
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/expectation_director.rb', line 69

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.



51
52
53
54
55
56
57
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/expectation_director.rb', line 51

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 directory. Check all the expectations. Only check the default expecatations if there are no non-default expectations.



62
63
64
65
66
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/expectation_director.rb', line 62

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