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

Methods

Public Class methods

[Source]

    # File vendor/plugins/rspec/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.

[Source]

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

[Source]

    # File vendor/plugins/rspec/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

[Source]

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

[Source]

    # File vendor/plugins/rspec/lib/spec/mocks/message_expectation.rb, line 65
65:       def and_yield(*args)
66:         @args_to_yield << args
67:         self
68:       end

[Source]

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

[Source]

    # File vendor/plugins/rspec/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

[Source]

    # File vendor/plugins/rspec/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

[Source]

     # File vendor/plugins/rspec/lib/spec/mocks/message_expectation.rb, line 123
123:       def invoke_consecutive_return_block(args, block)
124:         args << block unless block.nil?
125:         value = @return_block.call(*args)
126:         
127:         index = [@actual_received_count, value.size-1].min
128:         value[index]
129:       end

[Source]

     # File vendor/plugins/rspec/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

[Source]

     # File vendor/plugins/rspec/lib/spec/mocks/message_expectation.rb, line 131
131:       def invoke_return_block(args, block)
132:         args << block unless block.nil?
133:         # Ruby 1.9 - when we set @return_block to return values
134:         # regardless of arguments, any arguments will result in
135:         # a "wrong number of arguments" error
136:         if @ignore_args
137:           @return_block.call()
138:         else
139:           @return_block.call(*args)
140:         end
141:       end

[Source]

     # File vendor/plugins/rspec/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:         @args_to_yield.each do |args_to_yield_this_time|
116:           if block.arity > -1 && args_to_yield_this_time.length != block.arity
117:             @error_generator.raise_wrong_arity_error args_to_yield_this_time, block.arity
118:           end
119:           block.call(*args_to_yield_this_time)
120:         end
121:       end

[Validate]