Class | Spec::Mocks::BaseExpectation |
In: |
lib/spec/mocks/message_expectation.rb
|
Parent: | Object |
sym | [R] |
# 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
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.
# File lib/spec/mocks/message_expectation.rb, line 57 57: def and_raise(exception=Exception) 58: @exception_to_raise = exception 59: end
# 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
# File lib/spec/mocks/message_expectation.rb, line 61 61: def and_throw(symbol) 62: @symbol_to_throw = symbol 63: end
# File lib/spec/mocks/message_expectation.rb, line 65 65: def and_yield(*args) 66: @args_to_yield << args 67: self 68: end
# File lib/spec/mocks/message_expectation.rb, line 26 26: def expected_args 27: @args_expectation.args 28: end
# File lib/spec/mocks/message_expectation.rb, line 74 74: def invoke(args, block) 75: if @expected_received_count == 0 76: @actual_received_count += 1 77: @error_generator.raise_expectation_error @sym, @expected_received_count, @actual_received_count, *args 78: end 79: 80: @order_group.handle_order_constraint self 81: 82: begin 83: Kernel::raise @exception_to_raise unless @exception_to_raise.nil? 84: Kernel::throw @symbol_to_throw unless @symbol_to_throw.nil? 85: 86: 87: if !@method_block.nil? 88: default_return_val = invoke_method_block(args) 89: elsif @args_to_yield.size > 0 90: default_return_val = invoke_with_yield(block) 91: else 92: default_return_val = nil 93: end 94: 95: if @consecutive 96: return invoke_consecutive_return_block(args, block) 97: elsif @return_block 98: return invoke_return_block(args, block) 99: else 100: return default_return_val 101: end 102: ensure 103: @actual_received_count += 1 104: end 105: end
# 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
# File lib/spec/mocks/message_expectation.rb, line 131 131: def invoke_consecutive_return_block(args, block) 132: args << block unless block.nil? 133: value = @return_block.call(*args) 134: 135: index = [@actual_received_count, value.size-1].min 136: value[index] 137: end
# File lib/spec/mocks/message_expectation.rb, line 109 109: def invoke_method_block(args) 110: begin 111: @method_block.call(*args) 112: rescue => detail 113: @error_generator.raise_block_failed_error @sym, detail.message 114: end 115: end
# File lib/spec/mocks/message_expectation.rb, line 139 139: def invoke_return_block(args, block) 140: args << block unless block.nil? 141: # Ruby 1.9 - when we set @return_block to return values 142: # regardless of arguments, any arguments will result in 143: # a "wrong number of arguments" error 144: if @ignore_args 145: @return_block.call() 146: else 147: @return_block.call(*args) 148: end 149: end
# File lib/spec/mocks/message_expectation.rb, line 117 117: def invoke_with_yield(block) 118: if block.nil? 119: @error_generator.raise_missing_block_error @args_to_yield 120: end 121: value = nil 122: @args_to_yield.each do |args_to_yield_this_time| 123: if block.arity > -1 && args_to_yield_this_time.length != block.arity 124: @error_generator.raise_wrong_arity_error args_to_yield_this_time, block.arity 125: end 126: value = block.call(*args_to_yield_this_time) 127: end 128: value 129: end