Class Spec::Mocks::BaseExpectation
In: lib/spec/mocks/message_expectation.rb
Parent: Object

Methods

Attributes

sym  [R] 

Public Class methods

[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 = lambda {}
14:         @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 = nil
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.

[Source]

    # File lib/spec/mocks/message_expectation.rb, line 56
56:       def and_raise(exception=Exception)
57:         @exception_to_raise = exception
58:       end

[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:         if values.size == 0
33:           value = nil
34:         elsif values.size == 1
35:           value = values[0]
36:         else
37:           value = values
38:           @consecutive = true
39:           @expected_received_count = values.size if @expected_received_count != :any &&
40:                                                     @expected_received_count < values.size
41:         end
42:         @return_block = block_given? ? return_block : lambda { value }
43:       end

[Source]

    # File lib/spec/mocks/message_expectation.rb, line 60
60:       def and_throw(symbol)
61:         @symbol_to_throw = symbol
62:       end

[Source]

    # File lib/spec/mocks/message_expectation.rb, line 64
64:       def and_yield(*args)
65:         @args_to_yield = args
66:       end

[Source]

    # File lib/spec/mocks/message_expectation.rb, line 26
26:       def expected_args
27:         @args_expectation.args
28:       end

[Source]

    # File lib/spec/mocks/message_expectation.rb, line 72
72:       def invoke(args, block)
73:         @order_group.handle_order_constraint self
74: 
75:         begin
76:           if @exception_to_raise.class == Class
77:             @exception_instance_to_raise = @exception_to_raise.new
78:           else 
79:             @exception_instance_to_raise = @exception_to_raise
80:           end
81:           Kernel::raise @exception_to_raise unless @exception_to_raise.nil?
82:           Kernel::throw @symbol_to_throw unless @symbol_to_throw.nil?
83: 
84:           if !@method_block.nil?
85:             return invoke_method_block(args)
86:           elsif !@args_to_yield.nil?
87:             return invoke_with_yield(block)
88:           elsif @consecutive
89:             return invoke_consecutive_return_block(args, block)
90:           else
91:             return invoke_return_block(args, block)
92:           end
93:         ensure
94:           @received_count += 1
95:         end
96:       end

[Source]

    # File lib/spec/mocks/message_expectation.rb, line 68
68:       def matches(sym, args)
69:         @sym == sym and @args_expectation.check_args(args)
70:       end

Protected Instance methods

[Source]

     # File lib/spec/mocks/message_expectation.rb, line 118
118:       def invoke_consecutive_return_block(args, block)
119:         args << block unless block.nil?
120:         value = @return_block.call(*args)
121:         
122:         index = [@received_count, value.size-1].min
123:         value[index]
124:       end

[Source]

     # File lib/spec/mocks/message_expectation.rb, line 100
100:       def invoke_method_block(args)
101:         begin
102:           @method_block.call(*args)
103:         rescue => detail
104:           @error_generator.raise_block_failed_error @sym, detail.message
105:         end
106:       end

[Source]

     # File lib/spec/mocks/message_expectation.rb, line 126
126:       def invoke_return_block(args, block)
127:         args << block unless block.nil?
128:         value = @return_block.call(*args)
129:     
130:         value
131:       end

[Source]

     # File lib/spec/mocks/message_expectation.rb, line 108
108:       def invoke_with_yield(block)
109:         if block.nil?
110:           @error_generator.raise_missing_block_error @args_to_yield
111:         end
112:         if block.arity > -1 && @args_to_yield.length != block.arity
113:           @error_generator.raise_wrong_arity_error @args_to_yield, block.arity
114:         end
115:         block.call(*@args_to_yield)
116:       end

[Validate]