Class: Lookout::Reception

Inherits:
Object show all
Defined in:
lib/lookout-3.0/reception.rb

Overview

Method reception expectation builder. Method reception expectations can set expectations on what method is going to be called, with what arguments, and how many times it’s to take place.

Defined Under Namespace

Modules: Arguments Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.of(object, method, *args, &body) ⇒ Lookout::Reception

Returns A reception expectation expecting METHOD to be called on OBJECT with ARGS, using BODY as the method definition.

Parameters:

Returns:

  • (Lookout::Reception)

    A reception expectation expecting METHOD to be called on OBJECT with ARGS, using BODY as the method definition.



16
17
18
# File 'lib/lookout-3.0/reception.rb', line 16

def of(object, method, *args, &body)
  new(object, method, 1..1.0/0, *args, &body)
end

Instance Method Details

#at_least(times) ⇒ Lookout::Reception

Sets the minimum number of times that the method may be called.

Parameters:

  • times (Integer)

Returns:

  • (Lookout::Reception)

    A reception expectation with a minimum number of TIMES that the method may be called

Raises:

  • (ArgumentError)

    If TIMES < 1



67
68
69
70
71
72
73
# File 'lib/lookout-3.0/reception.rb', line 67

def at_least(times)
  limit('cannot convert lower reception limit to Integer: %s', times){ |i|
    raise ArgumentError,
      'lower reception limit must be positive: %d < 1' % i if i < 1
    i..1.0/0
  }
end

#at_least_onceObject

This is an alias for #at_least(1).



33
# File 'lib/lookout-3.0/reception.rb', line 33

def at_least_once; at_least(1) end

#at_most(times) ⇒ Lookout::Reception

Returns A reception expectation with a maximum number of TIMES that the method may be called.

Parameters:

  • times (Integer)

Returns:

  • (Lookout::Reception)

    A reception expectation with a maximum number of TIMES that the method may be called

Raises:

  • (ArgumentError)

    If TIMES < 1



42
43
44
45
46
47
48
# File 'lib/lookout-3.0/reception.rb', line 42

def at_most(times)
  limit('cannot convert upper reception limit to Integer: %s', times){ |i|
    raise ArgumentError,
      'upper reception limit must be positive: %d < 1' % i if i < 1
    0..i
  }
end

#at_most_onceObject

This is an alias for #at_most(1).



27
# File 'lib/lookout-3.0/reception.rb', line 27

def at_most_once; at_most(1) end

#block(&block) ⇒ Proc

Returns A block to be used by Expected::Lookout::Reception#expect that’ll set up a stub for the expected method reception that’ll check that the call count doesn’t exceed any upper limit imposed upon it and verify that any arguments are what they’re expected to be and then invoke BLOCK.

Returns:

  • (Proc)

    A block to be used by Expected::Lookout::Reception#expect that’ll set up a stub for the expected method reception that’ll check that the call count doesn’t exceed any upper limit imposed upon it and verify that any arguments are what they’re expected to be and then invoke BLOCK



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/lookout-3.0/reception.rb', line 86

def block(&block)
  args, calls = Arguments.for(*@args), 0
  reception, object, method, range, body = self, @object, @method, @range, @body || Nil
  proc{
    stub(object, method => proc{ |*mock_args, &mock_block|
           calls += 1
           raise Error.from(reception, calls, range) if calls > range.end
           raise Arguments::Error,
             '%s: unexpected arguments: [%s]≠[%s]' %
               [reception, Arguments::List.new(*mock_args), args] unless
                 args =~ mock_args
           body.call(*mock_args, &mock_block)
         }, &block) if block
    calls
  }
end

#exactly(times) ⇒ Lookout::Reception

Returns A reception expectation with a minimum and maximum number of TIMES that the method may be called.

Parameters:

  • times (Integer)

Returns:

  • (Lookout::Reception)

    A reception expectation with a minimum and maximum number of TIMES that the method may be called

Raises:

  • (ArgumentError)

    If TIMES < 0



54
55
56
57
58
59
60
# File 'lib/lookout-3.0/reception.rb', line 54

def exactly(times)
  limit('cannot convert reception invocation count to Integer: %s', times){ |i|
    raise ArgumentError,
      'expected reception count must be non-negative: %d < 0' % i if i < 0
    i..i
  }
end

#neverObject

This is an alias for #exactly(0).



24
# File 'lib/lookout-3.0/reception.rb', line 24

def never; exactly(0) end

#onceObject

This is an alias for #exactly(1).



30
# File 'lib/lookout-3.0/reception.rb', line 30

def once; exactly(1) end

#to_lookout_expectedExpected::Lookout::Reception

Returns A wrapper around the object that represents the expected reception of this method.

Returns:



77
78
79
# File 'lib/lookout-3.0/reception.rb', line 77

def to_lookout_expected
  Lookout::Expected::Lookout::Reception.new(self)
end

#to_sObject

Returns A String consisting of the inspection of the object, followed by either ‘.’ or ‘#’ depending on whether the object is a Class or not, and the method name.

Returns:

  • A String consisting of the inspection of the object, followed by either ‘.’ or ‘#’ depending on whether the object is a Class or not, and the method name



106
107
108
109
110
# File 'lib/lookout-3.0/reception.rb', line 106

def to_s
  [Lookout::Inspect.new(object, 'object'),
   Class === object ? '.' : '#',
   method].join('')
end

#twiceObject

This is an alias for #exactly(2).



36
# File 'lib/lookout-3.0/reception.rb', line 36

def twice; exactly(2) end