Class: FlexMock::CompositeExpectation

Inherits:
BasicObject
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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCompositeExpectation

Initialize the composite expectation.



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

def initialize
  @expectations = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Apply the constraint method to all expectations in the composite.



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

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

Instance Attribute Details

#expectationsObject (readonly)

Returns the value of attribute expectations.



7
8
9
# File 'lib/flexmock/composite_expectation.rb', line 7

def expectations
  @expectations
end

Instance Method Details

#add(expectation) ⇒ Object

Add an expectation to the composite.



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

def add(expectation)
  @expectations << expectation
end

#mockObject

Return the associated mock object.



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

def mock
  @expectations.first.mock
end

#order_numberObject

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



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

def order_number
  @expectations.first.order_number
end

#should_receive(*args, **kw, &block) ⇒ Object

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



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

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

#to_sObject

Return a string representations



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

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