Methods
- and_raise
- and_return
- and_throw
- and_yield
- expected_args
- invoke
- invoke_consecutive_return_block
- invoke_method_block
- invoke_return_block
- invoke_with_yield
- matches
- new
Attributes
| [R] | sym |
Public Class methods
new(error_generator, expectation_ordering, expected_from, sym, method_block, expected_received_count=1, opts={})
[ show source ]
# File lib/spec/mocks/message_expectation.rb, line 7
7: def initialize(error_generator, expectation_ordering, expected_from, sym, method_block, expected_received_count=1, opts={})
8: @error_generator = error_generator
9: @error_generator.opts = opts
10: @expected_from = expected_from
11: @sym = sym
12: @method_block = method_block
13: @return_block = nil
14: @actual_received_count = 0
15: @expected_received_count = expected_received_count
16: @args_expectation = ArgumentExpectation.new([AnyArgsConstraint.new])
17: @consecutive = false
18: @exception_to_raise = nil
19: @symbol_to_throw = nil
20: @order_group = expectation_ordering
21: @at_least = nil
22: @at_most = nil
23: @args_to_yield = []
24: end
Public Instance methods
Warning
When you pass an exception class, the MessageExpectation will raise an instance of it, creating it with new. If the exception class initializer requires any parameters, you must pass in an instance and not the class.
[ show source ]
# File lib/spec/mocks/message_expectation.rb, line 57
57: def and_raise(exception=Exception)
58: @exception_to_raise = exception
59: end
[ show source ]
# File lib/spec/mocks/message_expectation.rb, line 30
30: def and_return(*values, &return_block)
31: Kernel::raise AmbiguousReturnError unless @method_block.nil?
32: case values.size
33: when 0 then value = nil
34: when 1 then value = values[0]
35: else
36: value = values
37: @consecutive = true
38: @expected_received_count = values.size if !ignoring_args? &&
39: @expected_received_count < values.size
40: end
41: @return_block = block_given? ? return_block : lambda { value }
42: # Ruby 1.9 - see where this is used below
43: @ignore_args = !block_given?
44: end
[ show source ]
# File lib/spec/mocks/message_expectation.rb, line 61
61: def and_throw(symbol)
62: @symbol_to_throw = symbol
63: end
[ show source ]
# File lib/spec/mocks/message_expectation.rb, line 65
65: def and_yield(*args)
66: @args_to_yield << args
67: self
68: end
[ show source ]
# File lib/spec/mocks/message_expectation.rb, line 26
26: def expected_args
27: @args_expectation.args
28: end
[ show source ]
# File lib/spec/mocks/message_expectation.rb, line 74
74: def invoke(args, block)
75: @order_group.handle_order_constraint self
76:
77: begin
78: Kernel::raise @exception_to_raise unless @exception_to_raise.nil?
79: Kernel::throw @symbol_to_throw unless @symbol_to_throw.nil?
80:
81: if !@method_block.nil?
82: default_return_val = invoke_method_block(args)
83: elsif @args_to_yield.size > 0
84: default_return_val = invoke_with_yield(block)
85: else
86: default_return_val = nil
87: end
88:
89: if @consecutive
90: return invoke_consecutive_return_block(args, block)
91: elsif @return_block
92: return invoke_return_block(args, block)
93: else
94: return default_return_val
95: end
96: ensure
97: @actual_received_count += 1
98: end
99: end
[ show source ]
# File lib/spec/mocks/message_expectation.rb, line 70
70: def matches(sym, args)
71: @sym == sym and @args_expectation.check_args(args)
72: end
Protected Instance methods
[ show source ]
# File lib/spec/mocks/message_expectation.rb, line 125
125: def invoke_consecutive_return_block(args, block)
126: args << block unless block.nil?
127: value = @return_block.call(*args)
128:
129: index = [@actual_received_count, value.size-1].min
130: value[index]
131: end
[ show source ]
# File lib/spec/mocks/message_expectation.rb, line 103
103: def invoke_method_block(args)
104: begin
105: @method_block.call(*args)
106: rescue => detail
107: @error_generator.raise_block_failed_error @sym, detail.message
108: end
109: end
[ show source ]
# File lib/spec/mocks/message_expectation.rb, line 133
133: def invoke_return_block(args, block)
134: args << block unless block.nil?
135: # Ruby 1.9 - when we set @return_block to return values
136: # regardless of arguments, any arguments will result in
137: # a "wrong number of arguments" error
138: if @ignore_args
139: @return_block.call()
140: else
141: @return_block.call(*args)
142: end
143: end
[ show source ]
# File lib/spec/mocks/message_expectation.rb, line 111
111: def invoke_with_yield(block)
112: if block.nil?
113: @error_generator.raise_missing_block_error @args_to_yield
114: end
115: value = nil
116: @args_to_yield.each do |args_to_yield_this_time|
117: if block.arity > -1 && args_to_yield_this_time.length != block.arity
118: @error_generator.raise_wrong_arity_error args_to_yield_this_time, block.arity
119: end
120: value = block.call(*args_to_yield_this_time)
121: end
122: value
123: end