Class: FlexMock::CompositeExpectation

Inherits:
Object
  • Object
show all
Defined in:
lib/flexmock/composite_expectation.rb

Overview

A composite expectation allows several expectations to be grouped into a single composite and then apply the same constraints to all expectations in the group.

Instance Method Summary collapse

Constructor Details

#initializeCompositeExpectation

Initialize the composite expectation.



9
10
11
# File 'lib/flexmock/composite_expectation.rb', line 9

def initialize
  @expectations = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Apply the constraint method to all expectations in the composite.



19
20
21
22
23
24
# File 'lib/flexmock/composite_expectation.rb', line 19

def method_missing(sym, *args, &block)
  @expectations.each do |expectation|
    expectation.send(sym, *args, &block)
  end
  self
end

Instance Method Details

#add(expectation) ⇒ Object

Add an expectation to the composite.



14
15
16
# File 'lib/flexmock/composite_expectation.rb', line 14

def add(expectation)
  @expectations << expectation
end

#mockObject

Return the associated mock object.



35
36
37
# File 'lib/flexmock/composite_expectation.rb', line 35

def mock
  @expectations.first.mock
end

#order_numberObject

Return the order number of the first expectation in the list.



30
31
32
# File 'lib/flexmock/composite_expectation.rb', line 30

def order_number
  @expectations.first.order_number
end

#should_receive(*args, &block) ⇒ Object

Start a new method expectation. The following constraints will be applied to the new expectation.



41
42
43
44
# File 'lib/flexmock/composite_expectation.rb', line 41

def should_receive(*args, &block)
  @expectations.first.mock.
    flexmock_define_expectation(caller.first, *args, &block)
end

#to_sObject

Return a string representations



47
48
49
50
51
52
53
# File 'lib/flexmock/composite_expectation.rb', line 47

def to_s
  if @expectations.size > 1
    "[" + @expectations.collect { |e| e.to_s }.join(', ') + "]"
  else
    @expectations.first.to_s
  end
end