Class: Lookout::Reception
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
-
.of(object, method, *args, &body) ⇒ Lookout::Reception
A reception expectation expecting METHOD to be called on OBJECT with ARGS, using BODY as the method definition.
Instance Method Summary collapse
-
#at_least(times) ⇒ Lookout::Reception
Sets the minimum number of times that the method may be called.
-
#at_least_once ⇒ Object
This is an alias for #at_least(1).
-
#at_most(times) ⇒ Lookout::Reception
A reception expectation with a maximum number of TIMES that the method may be called.
-
#at_most_once ⇒ Object
This is an alias for #at_most(1).
-
#block(&block) ⇒ 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.
-
#exactly(times) ⇒ Lookout::Reception
A reception expectation with a minimum and maximum number of TIMES that the method may be called.
-
#never ⇒ Object
This is an alias for #exactly(0).
-
#once ⇒ Object
This is an alias for #exactly(1).
-
#to_lookout_expected ⇒ Expected::Lookout::Reception
A wrapper around the object that represents the expected reception of this method.
-
#to_s ⇒ Object
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.
-
#twice ⇒ Object
This is an alias for #exactly(2).
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.
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.
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_once ⇒ Object
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.
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_once ⇒ Object
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.
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.
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 |
#never ⇒ Object
This is an alias for #exactly(0).
24 |
# File 'lib/lookout-3.0/reception.rb', line 24 def never; exactly(0) end |
#once ⇒ Object
This is an alias for #exactly(1).
30 |
# File 'lib/lookout-3.0/reception.rb', line 30 def once; exactly(1) end |
#to_lookout_expected ⇒ Expected::Lookout::Reception
Returns A wrapper around the object that represents the expected reception of this method.
77 78 79 |
# File 'lib/lookout-3.0/reception.rb', line 77 def to_lookout_expected Lookout::Expected::Lookout::Reception.new(self) end |
#to_s ⇒ Object
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 |